Rotate Effects¶
Rotate effects are used to change the orientation of a component over time. They can be used to make
a component spin, turn towards a target, or rotate around a point. The rotation is specified in
radians, and the effects can be applied to any component that has a rotation property, such as the
PositionComponent
.
RotateEffect.by
¶
Rotates the target clockwise by the specified angle relative to its current orientation. The angle is in radians. For example, the following effect will rotate the target 90º (=tau/4 in radians) clockwise:
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/effects.dart';
3import 'package:flame/game.dart';
4import 'package:flame/geometry.dart';
5
6class RotateByEffectGame extends FlameGame {
7 bool reset = false;
8
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 RotateEffect.by(
17 reset ? tau / 4 : -tau / 4,
18 EffectController(duration: 2),
19 ),
20 );
21 reset = !reset;
22 },
23 );
24 add(flower);
25 }
26}
final effect = RotateEffect.by(
tau/4,
EffectController(duration: 2),
);
RotateEffect.to
¶
Rotates the target clockwise to the specified angle. For example, the following will rotate the target to look east (0º is north, 90º=tau/4 east, 180º=tau/2 south, and 270º=tau*3/4 west):
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/effects.dart';
3import 'package:flame/game.dart';
4import 'package:flame/geometry.dart';
5
6class RotateToEffectGame extends FlameGame {
7 bool reset = false;
8
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 RotateEffect.to(
17 reset ? tau / 4 : -tau / 4,
18 EffectController(duration: 2),
19 ),
20 );
21 reset = !reset;
22 },
23 );
24 add(flower);
25 }
26}
final effect = RotateEffect.to(
tau/4,
EffectController(duration: 2),
);
RotateAroundEffect
¶
Rotates the target clockwise by the specified angle relative to its current orientation around the specified center. The angle is in radians. For example, the following effect will rotate the target 90º (=tau/4 in radians) clockwise around (100, 100).
1import 'package:doc_flame_examples/flower.dart';
2import 'package:flame/effects.dart';
3import 'package:flame/game.dart';
4import 'package:flame/geometry.dart';
5
6class RotateAroundEffectGame extends FlameGame {
7 bool reset = false;
8
9 @override
10 Future<void> onLoad() async {
11 final flower = Flower(
12 size: 60,
13 position: canvasSize / 4,
14 onTap: (flower) {
15 flower.add(
16 RotateAroundEffect(
17 reset != reset ? tau : -tau,
18 center: canvasSize / 2,
19 EffectController(duration: 1),
20 ),
21 );
22 reset = !reset;
23 },
24 );
25 add(flower);
26 }
27}
final effect = RotateAroundEffect(
tau/4,
center: Vector2(100, 100),
EffectController(duration: 2),
);