mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-07 06:57:22 +08:00
feat: The FunctionEffect, run any function as an Effect (#3537)
The `FunctionEffect` is super simple, but very powerful. It makes it possible to run any function over time as an effect without having to create a new effect. This is very useful when for example doing state changes over time.
This commit is contained in:
64
examples/lib/stories/effects/function_effect_example.dart
Normal file
64
examples/lib/stories/effects/function_effect_example.dart
Normal file
@ -0,0 +1,64 @@
|
||||
import 'package:flame/components.dart';
|
||||
import 'package:flame/effects.dart';
|
||||
import 'package:flame/game.dart';
|
||||
import 'package:flame/input.dart';
|
||||
|
||||
enum RobotState {
|
||||
idle,
|
||||
running,
|
||||
}
|
||||
|
||||
class FunctionEffectExample extends FlameGame with TapDetector {
|
||||
static const String description = '''
|
||||
This example shows how to use the FunctionEffect to create custom effects.
|
||||
|
||||
The robot will switch between running and idle animations over the duration of
|
||||
10 seconds.
|
||||
''';
|
||||
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
final running = await loadSpriteAnimation(
|
||||
'animations/robot.png',
|
||||
SpriteAnimationData.sequenced(
|
||||
amount: 8,
|
||||
stepTime: 0.2,
|
||||
textureSize: Vector2(16, 18),
|
||||
),
|
||||
);
|
||||
final idle = await loadSpriteAnimation(
|
||||
'animations/robot-idle.png',
|
||||
SpriteAnimationData.sequenced(
|
||||
amount: 4,
|
||||
stepTime: 0.4,
|
||||
textureSize: Vector2(16, 18),
|
||||
),
|
||||
);
|
||||
final robotSize = Vector2(64, 72);
|
||||
|
||||
final functionEffect =
|
||||
FunctionEffect<SpriteAnimationGroupComponent<RobotState>>(
|
||||
(target, progress) {
|
||||
if (progress > 0.7) {
|
||||
target.current = RobotState.idle;
|
||||
} else if (progress > 0.3) {
|
||||
target.current = RobotState.running;
|
||||
}
|
||||
},
|
||||
EffectController(duration: 10.0, infinite: true),
|
||||
);
|
||||
final component = SpriteAnimationGroupComponent<RobotState>(
|
||||
animations: {
|
||||
RobotState.running: running,
|
||||
RobotState.idle: idle,
|
||||
},
|
||||
current: RobotState.idle,
|
||||
position: size / 2,
|
||||
anchor: Anchor.center,
|
||||
size: robotSize,
|
||||
children: [functionEffect],
|
||||
);
|
||||
|
||||
add(component);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user