Skip to content

Commit

Permalink
fix: RemoveEffect should work within SequenceEffect (#2110)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
spydon authored Oct 24, 2022
1 parent f7c2f54 commit 03e1f33
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 3 additions & 4 deletions packages/flame/lib/src/effects/remove_effect.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'package:flame/src/effects/controllers/linear_effect_controller.dart';
import 'package:flame/src/effects/effect.dart';
import 'package:flame/effects.dart';

/// This simple effect, when attached to a component, will cause that component
/// to be removed from the game tree after `delay` seconds.
class RemoveEffect extends Effect {
class RemoveEffect extends ComponentEffect {
RemoveEffect({
double delay = 0.0,
void Function()? onComplete,
Expand All @@ -15,7 +14,7 @@ class RemoveEffect extends Effect {
@override
void apply(double progress) {
if (progress == 1) {
parent?.removeFromParent();
target.removeFromParent();
}
}
}
21 changes: 20 additions & 1 deletion packages/flame/test/effects/remove_effect_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flame/components.dart';
import 'package:flame/effects.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';

void main() {
Expand Down Expand Up @@ -41,5 +42,23 @@ void main() {
game.update(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);
});
});
}

0 comments on commit 03e1f33

Please sign in to comment.