diff --git a/lib/game/pinball_game.dart b/lib/game/pinball_game.dart index 9794f1ff..c09d74cc 100644 --- a/lib/game/pinball_game.dart +++ b/lib/game/pinball_game.dart @@ -87,7 +87,7 @@ class PinballGame extends Forge2DGame @override void onTapDown(TapDownInfo info) { if (info.raw.kind == PointerDeviceKind.touch) { - final rocket = children.whereType().first; + final rocket = descendants().whereType().first; final bounds = rocket.topLeftPosition & rocket.size; // NOTE(wolfen): As long as Flame does not have https://github.com/flame-engine/flame/issues/1586 we need to check it at the highest level manually. @@ -153,23 +153,27 @@ class _GameBallsController extends ComponentController @override void onNewState(GameState state) { super.onNewState(state); - _spawnBall(); + spawnBall(); } @override - void onMount() { - super.onMount(); - _spawnBall(); + Future onLoad() async { + await super.onLoad(); + spawnBall(); } - void _spawnBall() { - final ball = ControlledBall.launch( - characterTheme: component.characterTheme, - )..initialPosition = Vector2( - Vector2(41.1, 43).x, - Vector2(41.1, 45).y - Ball.size.y, - ); - component.firstChild()?.add(ball); + void spawnBall() { + // TODO(alestiago): Refactor with behavioural pattern. + component.ready().whenComplete(() { + final plunger = parent!.descendants().whereType().single; + final ball = ControlledBall.launch( + characterTheme: component.characterTheme, + )..initialPosition = Vector2( + plunger.body.position.x, + plunger.body.position.y - Ball.size.y, + ); + component.firstChild()?.add(ball); + }); } } @@ -181,7 +185,7 @@ class DebugPinballGame extends PinballGame with FPSCounter { characterTheme: characterTheme, audio: audio, ) { - controller = _DebugGameBallsController(this); + controller = _GameBallsController(this); } @override @@ -202,10 +206,6 @@ class DebugPinballGame extends PinballGame with FPSCounter { } } -class _DebugGameBallsController extends _GameBallsController { - _DebugGameBallsController(PinballGame game) : super(game); -} - // TODO(wolfenrain): investigate this CI failure. // coverage:ignore-start class _DebugInformation extends Component with HasGameRef {