From 1eae30dfd561b3db1d567895cc085d17dc053642 Mon Sep 17 00:00:00 2001 From: DevKage Date: Sun, 25 Feb 2024 15:14:30 +0530 Subject: [PATCH 1/3] Check for removing state while adding child --- packages/flame/lib/src/components/core/component.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/components/core/component.dart b/packages/flame/lib/src/components/core/component.dart index 0097139dea6..d16ddf6a0b2 100644 --- a/packages/flame/lib/src/components/core/component.dart +++ b/packages/flame/lib/src/components/core/component.dart @@ -594,7 +594,7 @@ class Component { _clearRemovingBit(); } game.enqueueMove(child, this); - } else if (isMounted && !child.isMounted) { + } else if (isMounted && !isRemoving && !child.isMounted) { child._parent = this; game.enqueueAdd(child, this); } else { From a77543a79b46c1c48682c79becd2e0b4691ecfb8 Mon Sep 17 00:00:00 2001 From: DevKage Date: Sun, 25 Feb 2024 16:06:05 +0530 Subject: [PATCH 2/3] Add a test --- .../flame/test/components/component_test.dart | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/flame/test/components/component_test.dart b/packages/flame/test/components/component_test.dart index 3dd96e61cbc..0d709ec97d3 100644 --- a/packages/flame/test/components/component_test.dart +++ b/packages/flame/test/components/component_test.dart @@ -556,6 +556,27 @@ void main() { expect(wrapper.contains(child), true); }, ); + + testWithFlameGame('when parent is in removing state', (game) async { + final parent = Component(); + final child = Component(); + + await game.add(parent); + await game.ready(); + + // Remove the parent and add the child in the same tick. + parent.removeFromParent(); + await parent.add(child); + + // Timeout is added because processLifecycleEvents of ComponentTreeRoot + // gets blocked in such cases. + expect(game.ready().timeout(const Duration(seconds: 2)), completes); + + // Readding the parent should eventually mount the child as well. + await game.add(parent); + await game.ready(); + expect(child.isMounted, true); + }); }); group('Removing components', () { From 04887154ace69e898f79390908b2bb0d97948ba6 Mon Sep 17 00:00:00 2001 From: DevKage Date: Sun, 25 Feb 2024 16:10:57 +0530 Subject: [PATCH 3/3] Fix spell check error --- packages/flame/test/components/component_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/test/components/component_test.dart b/packages/flame/test/components/component_test.dart index 0d709ec97d3..1eb61d6aa81 100644 --- a/packages/flame/test/components/component_test.dart +++ b/packages/flame/test/components/component_test.dart @@ -572,7 +572,7 @@ void main() { // gets blocked in such cases. expect(game.ready().timeout(const Duration(seconds: 2)), completes); - // Readding the parent should eventually mount the child as well. + // Adding the parent again should eventually mount the child as well. await game.add(parent); await game.ready(); expect(child.isMounted, true);