jsb:flame_parallaxcomponent
Flame ParallaxComponent用于设置动态背景
用于色差的原理,不同的图片,可以设置不同的速度,形成动态背景。
1 最简单的方法:
可以在某个 GameWidget 中设置:
- snippet.dart
@override Future<void> onLoad() async { final parallaxComponent = await loadParallaxComponent([ ParallaxImageData('bg.png'), ParallaxImageData('trees.png'), ]); add(parallaxComponent); }
这种应该设置为静态的背景。
如果用 component 自己加载,则可以用下面这个方法:
- snippet.dart
class MyParallaxComponent extends ParallaxComponent<MyGame> { @override Future<void> onLoad() async { parallax = await gameRef.loadParallax([ ParallaxImageData('bg.png'), ParallaxImageData('trees.png'), ]); } } class MyGame extends FlameGame { @override void onLoad() { add(MyParallaxComponent()); } }
2 动态的背景
- snippet.dart
@override Future<void> onLoad() async { final parallaxComponent = await loadParallaxComponent( _dataList, baseVelocity: Vector2(20, 0), velocityMultiplierDelta: Vector2(1.8, 1.0), ); }
也可以随时 load
- snippet.cpp
@override void onLoad() { final parallax = parallaxComponent.parallax; parallax.baseSpeed = Vector2(100, 0); parallax.velocityMultiplierDelta = Vector2(2.0, 1.0); }
更高级的设置方法:
- snippet.dart
final images = [ loadParallaxImage( 'stars.jpg', repeat: ImageRepeat.repeat, alignment: Alignment.center, fill: LayerFill.width, ), loadParallaxImage( 'planets.jpg', repeat: ImageRepeat.repeatY, alignment: Alignment.bottomLeft, fill: LayerFill.none, ), loadParallaxImage( 'dust.jpg', repeat: ImageRepeat.repeatX, alignment: Alignment.topRight, fill: LayerFill.height, ), ]; final layers = images.map( (image) => ParallaxLayer( await image, velocityMultiplier: images.indexOf(image) * 2.0, ) ); final parallaxComponent = ParallaxComponent.fromParallax( Parallax( await Future.wait(layers), baseVelocity: Vector2(50, 0), ), );
3 space shootter 的设置:
- snippet.dart
@override Future<void> onLoad() async { final parallax = await loadParallaxComponent( [ ParallaxImageData('stars_0.png'), ParallaxImageData('stars_1.png'), ParallaxImageData('stars_2.png'), ], baseVelocity: Vector2(0, -5), //基本的速度,向下 repeat: ImageRepeat.repeat, velocityMultiplierDelta: Vector2(0, 5),//不同的图片,速度不一样 ); add(parallax); }
jsb/flame_parallaxcomponent.txt · 最后更改: 2024/06/25 16:25 由 lanshizi