mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-03 20:36:31 +08:00
onComplete callback for effects
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
## [next]
|
||||
- Adding BaseGame#markToRemove
|
||||
- Upgrade tiled and flutter_svg dependencies
|
||||
- onComplete callback for effects
|
||||
|
||||
## 0.22.1
|
||||
- Fix Box2DComponent render priority
|
||||
|
||||
@ -14,6 +14,8 @@ If `isInfinite` is false and `isAlternating` is true the effect will go from the
|
||||
|
||||
`isInfinite` and `isAlternating` are false by default and then the effect is just applied once, from the beginning of the curve until the end.
|
||||
|
||||
When an effect is completed the callback `onComplete` will be called, it can be set as an optional argument to your effect.
|
||||
|
||||
## MoveEffect
|
||||
|
||||
Applied to `PositionComponent`s, this effect can be used to move the component to a new position, using an [animation curve](https://api.flutter.dev/flutter/animation/Curves-class.html).
|
||||
|
||||
@ -65,6 +65,7 @@ class MyGame extends BaseGame with TapDetector {
|
||||
isInfinite: false,
|
||||
isAlternating: true,
|
||||
offset: 0.5,
|
||||
onComplete: () => print("onComplete callback"),
|
||||
);
|
||||
greenSquare.addEffect(combination);
|
||||
}
|
||||
|
||||
@ -14,7 +14,8 @@ class CombinedEffect extends PositionComponentEffect {
|
||||
this.offset = 0.0,
|
||||
bool isInfinite = false,
|
||||
bool isAlternating = false,
|
||||
}) : super(isInfinite, isAlternating) {
|
||||
Function onComplete,
|
||||
}) : super(isInfinite, isAlternating, onComplete: onComplete) {
|
||||
final types = effects.map((e) => e.runtimeType);
|
||||
assert(
|
||||
types.toSet().length == types.length,
|
||||
|
||||
@ -12,6 +12,7 @@ export './sequence_effect.dart';
|
||||
|
||||
abstract class PositionComponentEffect {
|
||||
PositionComponent component;
|
||||
Function() onComplete;
|
||||
|
||||
bool _isDisposed = false;
|
||||
bool get isDisposed => _isDisposed;
|
||||
@ -37,7 +38,11 @@ abstract class PositionComponentEffect {
|
||||
/// travel time
|
||||
double get totalTravelTime => travelTime * (isAlternating ? 2 : 1);
|
||||
|
||||
PositionComponentEffect(this._initialIsInfinite, this._initialIsAlternating) {
|
||||
PositionComponentEffect(
|
||||
this._initialIsInfinite,
|
||||
this._initialIsAlternating, {
|
||||
this.onComplete,
|
||||
}) {
|
||||
isInfinite = _initialIsInfinite;
|
||||
isAlternating = _initialIsAlternating;
|
||||
}
|
||||
@ -53,6 +58,9 @@ abstract class PositionComponentEffect {
|
||||
if (!hasFinished()) {
|
||||
currentTime += dt * curveDirection + driftTime * driftMultiplier;
|
||||
percentage = min(1.0, max(0.0, currentTime / travelTime));
|
||||
if (hasFinished()){
|
||||
onComplete?.call();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -28,7 +28,8 @@ class MoveEffect extends PositionComponentEffect {
|
||||
this.curve,
|
||||
isInfinite = false,
|
||||
isAlternating = false,
|
||||
}) : super(isInfinite, isAlternating);
|
||||
Function onComplete
|
||||
}) : super(isInfinite, isAlternating, onComplete: onComplete);
|
||||
|
||||
@override
|
||||
void initialize(_comp) {
|
||||
|
||||
@ -18,7 +18,8 @@ class RotateEffect extends PositionComponentEffect {
|
||||
this.curve,
|
||||
isInfinite = false,
|
||||
isAlternating = false,
|
||||
}) : super(isInfinite, isAlternating);
|
||||
Function onComplete,
|
||||
}) : super(isInfinite, isAlternating, onComplete: onComplete);
|
||||
|
||||
@override
|
||||
void initialize(_comp) {
|
||||
|
||||
@ -25,7 +25,8 @@ class ScaleEffect extends PositionComponentEffect {
|
||||
this.curve,
|
||||
isInfinite = false,
|
||||
isAlternating = false,
|
||||
}) : super(isInfinite, isAlternating);
|
||||
Function onComplete,
|
||||
}) : super(isInfinite, isAlternating, onComplete: onComplete);
|
||||
|
||||
@override
|
||||
void initialize(_comp) {
|
||||
|
||||
@ -14,7 +14,8 @@ class SequenceEffect extends PositionComponentEffect {
|
||||
@required this.effects,
|
||||
isInfinite = false,
|
||||
isAlternating = false,
|
||||
}) : super(isInfinite, isAlternating) {
|
||||
Function onComplete,
|
||||
}) : super(isInfinite, isAlternating, onComplete: onComplete) {
|
||||
assert(
|
||||
effects.every((effect) => effect.component == null),
|
||||
"No effects can be added to components from the start",
|
||||
|
||||
Reference in New Issue
Block a user