mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 10:38:17 +08:00
fix: RemoveEffect should work within SequenceEffect (#2110)
Since the RemoveEffect was removing its parent and not its target it couldn't work within a SequenceEffect since it was just removing that effect instead.
This commit is contained in:
@ -1,9 +1,8 @@
|
|||||||
import 'package:flame/src/effects/controllers/linear_effect_controller.dart';
|
import 'package:flame/effects.dart';
|
||||||
import 'package:flame/src/effects/effect.dart';
|
|
||||||
|
|
||||||
/// This simple effect, when attached to a component, will cause that component
|
/// This simple effect, when attached to a component, will cause that component
|
||||||
/// to be removed from the game tree after `delay` seconds.
|
/// to be removed from the game tree after `delay` seconds.
|
||||||
class RemoveEffect extends Effect {
|
class RemoveEffect extends ComponentEffect {
|
||||||
RemoveEffect({
|
RemoveEffect({
|
||||||
double delay = 0.0,
|
double delay = 0.0,
|
||||||
void Function()? onComplete,
|
void Function()? onComplete,
|
||||||
@ -15,7 +14,7 @@ class RemoveEffect extends Effect {
|
|||||||
@override
|
@override
|
||||||
void apply(double progress) {
|
void apply(double progress) {
|
||||||
if (progress == 1) {
|
if (progress == 1) {
|
||||||
parent?.removeFromParent();
|
target.removeFromParent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import 'package:flame/components.dart';
|
import 'package:flame/components.dart';
|
||||||
|
import 'package:flame/effects.dart';
|
||||||
import 'package:flame/game.dart';
|
import 'package:flame/game.dart';
|
||||||
import 'package:flame/src/effects/remove_effect.dart';
|
import 'package:flame_test/flame_test.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -41,5 +42,23 @@ void main() {
|
|||||||
game.update(0);
|
game.update(0);
|
||||||
expect(game.children.length, 0);
|
expect(game.children.length, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWithFlameGame('as a part of a sequence', (game) async {
|
||||||
|
final component = PositionComponent();
|
||||||
|
await game.ensureAdd(component);
|
||||||
|
component.add(
|
||||||
|
SequenceEffect([
|
||||||
|
MoveByEffect(Vector2.all(10), EffectController(duration: 1)),
|
||||||
|
RemoveEffect(),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
game.update(0);
|
||||||
|
expect(game.children.length, 1);
|
||||||
|
game.update(0.5);
|
||||||
|
expect(game.children.length, 1);
|
||||||
|
game.update(1.0); // This completes the move effect
|
||||||
|
game.update(0); // This runs the remove effect
|
||||||
|
expect(game.children.length, 0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user