diff --git a/lib/flame/component_controller.dart b/lib/flame/component_controller.dart index 2bbf5ca9..bc88094c 100644 --- a/lib/flame/component_controller.dart +++ b/lib/flame/component_controller.dart @@ -22,6 +22,11 @@ abstract class ComponentController extends Component { ); await super.addToParent(parent); } + + @override + Future add(Component component) { + throw Exception('ComponentController should not add other components.'); + } } /// Mixin that attaches a single [ComponentController] to a [Component]. diff --git a/test/flame/component_controller_test.dart b/test/flame/component_controller_test.dart index 4e5da210..0df9719b 100644 --- a/test/flame/component_controller_test.dart +++ b/test/flame/component_controller_test.dart @@ -31,6 +31,7 @@ void main() { ); }, ); + flameTester.test( 'throws AssertionError when not attached to controlled component', (game) async { @@ -44,6 +45,20 @@ void main() { ); }, ); + + flameTester.test( + 'throws Exception when adding other component', + (game) async { + final component = ControlledComponent(); + final controller = TestComponentController(component); + + final anotherComponet = Component(); + await expectLater( + () async => controller.add(anotherComponet), + throwsException, + ); + }, + ); }); group('Controls', () {