Move Effects¶
Move Effects are a special type of effects that modify the position of a component over time, if you want to for example move your character from one point to another, make it jump, or follow a path, then you can use one of the predefined move effects.
MoveByEffect
¶
This effect applies to a PositionComponent
and shifts it by a prescribed offset
amount. This
offset is relative to the current position of the target:
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/effects.dart';
3import 'package:flame/game.dart';
4
5class MoveByEffectGame extends FlameGame {
6 bool reset = false;
7 @override
8 Future<void> onLoad() async {
9 final flower = Flower(
10 size: 60,
11 position: canvasSize / 2,
12 onTap: (flower) {
13 flower.add(
14 MoveEffect.by(
15 reset ? Vector2(-30, -30) : Vector2(30, 30),
16 EffectController(duration: 1.0),
17 ),
18 );
19 reset = !reset;
20 },
21 );
22 add(flower);
23 }
24}
final effect = MoveByEffect(
Vector2(0, -10),
EffectController(duration: 0.5),
);
If the component is currently at Vector2(250, 200)
, then at the end of the effect its position
will be Vector2(250, 190)
.
Multiple move effects can be applied to a component at the same time. The result will be the superposition of all the individual effects.
MoveToEffect
¶
This effect moves a PositionComponent
from its current position to the specified destination
point in a straight line.
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/effects.dart';
3import 'package:flame/game.dart';
4
5class MoveToEffectGame extends FlameGame {
6 bool reset = false;
7
8 @override
9 Future<void> onLoad() async {
10 final flower = Flower(
11 size: 60,
12 position: canvasSize / 2,
13 onTap: (flower) {
14 flower.add(
15 MoveEffect.to(
16 reset ? size / 2 : Vector2(30, 30),
17 EffectController(duration: 1.0),
18 ),
19 );
20 reset = !reset;
21 },
22 );
23 add(flower);
24 }
25}
final effect = MoveToEffect(
Vector2(100, 500),
EffectController(duration: 3),
);
It is possible, but not recommended to attach multiple such effects to the same component.
MoveAlongPathEffect
¶
This effect moves a PositionComponent
along the specified path relative to the component’s
current position. The path can have non-linear segments, but must be singly connected. It is
recommended to start a path at Vector2.zero()
in order to avoid sudden jumps in the component’s
position.
1import 'dart:ui';
2
3import 'package:doc_flame_examples/flower.dart';
4import 'package:flame/effects.dart';
5import 'package:flame/game.dart';
6
7class MoveAlongPathEffectGame extends FlameGame {
8 bool reset = false;
9 @override
10 Future<void> onLoad() async {
11 final flower = Flower(
12 size: 60,
13 position: canvasSize / 2,
14 onTap: (flower) {
15 flower.add(
16 MoveAlongPathEffect(
17 reset
18 ? (Path()..quadraticBezierTo(-100, 0, -50, 50))
19 : (Path()..quadraticBezierTo(100, 0, 50, -50)),
20 EffectController(duration: 1.5),
21 ),
22 );
23 reset = !reset;
24 },
25 );
26 add(flower);
27 }
28}
final effect = MoveAlongPathEffect(
Path()..quadraticBezierTo(100, 0, 50, -50),
EffectController(duration: 1.5),
);
An optional flag absolute: true
will declare the path within the effect as absolute. That is, the
target will “jump” to the beginning of the path at start, and then follow that path as if it was a
curve drawn on the canvas.
Another flag oriented: true
instructs the target not only move along the curve, but also rotate
itself in the direction the curve is facing at each point. With this flag the effect becomes both
the move- and the rotate- effect at the same time.