Refactors setting of last score in demo game to use callback

This commit is contained in:
Viktor Lidholt
2015-07-29 13:33:10 -07:00
parent 430c08f3e6
commit 6aa8e4bb3f
2 changed files with 32 additions and 30 deletions

View File

@@ -51,7 +51,9 @@ class GameDemoWorld extends NodeWithSize {
// Heads up display
Hud _hud;
GameDemoWorld(App app, this._navigator, ImageMap images, this._spriteSheet, this._spriteSheetUI) : super(new Size(_gameSizeWidth, _gameSizeHeight)) {
Function _gameOverCallback;
GameDemoWorld(App app, this._navigator, ImageMap images, this._spriteSheet, this._spriteSheetUI, this._gameOverCallback) : super(new Size(_gameSizeWidth, _gameSizeHeight)) {
// Fetch images
_imgNebula = images["assets/nebula.png"];
@@ -348,7 +350,7 @@ class GameDemoWorld extends NodeWithSize {
// Set game over
_isGameOver = true;
lastScore = _hud.score;
_gameOverCallback(_hud.score);
// Remove the ship
_ship.visible = false;

View File

@@ -54,6 +54,7 @@ class GameDemoApp extends App {
NavigationState _navigationState;
GameDemoWorld _game;
int _lastScore = 0;
void initState() {
_navigationState = new NavigationState([
@@ -93,38 +94,37 @@ class GameDemoApp extends App {
Widget _buildMainScene(navigator, route) {
return new Stack([
new SpriteWidget(new MainScreenBackground()),
new Center(
child: new Flex([
new TextureButton(
onPressed: () {
_game = new GameDemoWorld(
_app,
navigator,
_loader,
_spriteSheet,
_spriteSheetUI
);
navigator.pushNamed('/game');
},
texture: _spriteSheetUI["btn_play_up.png"],
textureDown: _spriteSheetUI["btn_play_down.png"],
width: 128.0,
height: 128.0
),
new Text(
"Last Score: $lastScore",
style: new TextStyle(fontSize:20.0)
)
],
direction: FlexDirection.vertical,
justifyContent: FlexJustifyContent.center)
)
new Flex([
new TextureButton(
onPressed: () {
_game = new GameDemoWorld(
_app,
navigator,
_loader,
_spriteSheet,
_spriteSheetUI,
(lastScore) {
setState(() {_lastScore = lastScore;});
}
);
navigator.pushNamed('/game');
},
texture: _spriteSheetUI["btn_play_up.png"],
textureDown: _spriteSheetUI["btn_play_down.png"],
width: 128.0,
height: 128.0
),
new Text(
"Last Score: $_lastScore",
style: new TextStyle(fontSize:20.0)
)
],
direction: FlexDirection.vertical,
justifyContent: FlexJustifyContent.center)
]);
}
}
int lastScore = 0;
class TextureButton extends ButtonBase {
TextureButton({
Key key,