Game Development Reference
In-Depth Information
playerTile.x = 50;
playerTile.y = 100;
enemyTile.x = 525;
enemyTile.y = 100;
this.addChild(playerTile);
this.addChild(enemyTile);
Finally, we send off events to update the scoreboard. playerLife and computerLife are two new
values that we track in Dice Battle that we set here in newLevel . We also need to determine
whose turn it is to play, but we will do that in the new game state to
GameStates.STATE_CHANGE_TURN . It which will decide which player (the human or computer) gets
to play the board next.
dispatchEvent(new CustomEventLevelScreenUpdate(
CustomEventLevelScreenUpdate.UPDATE_TEXT, String(level)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_SCORE,String(score)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_LEVEL,String(level)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_COMPUTER_LIFE,
String(computerLife)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT,Main.SCORE_BOARD_PLAYER_LIFE,
String(playerLife)));
gameState = GameStates.STATE_CHANGE_TURN;
}
Taking turns using new game states
The runGame function of Dice Battle has been updated to account for the fact that two players take
turns in this game, the human player and the computer player. While this is not a two-player hot-
seat game, with just a few minor changes it could easily be one. We will run through a short
description of the major changes after you take a look at the following code:
override public function runGame():void {
switch(gameState) {
case GameStates.STATE_INITIALIZING:
break;
The new gameState , GameStates.STATE_CHANGE_TURN was set in the newLevel function. Since turn
is set to an empty string ( "" ) when the level starts, the first turn is always "Player" . From then on,
we simply switch between the value of TURN_PLAYER and the value of TURN_COMPUTER until the level
or game is over. We also dispatch an event to the scoreboard to update the Turn text at the
bottom of the screen.
case GameStates.STATE_CHANGE_TURN:
if (turn == TURN_COMPUTER || turn == "") {
turn = TURN_PLAYER;
} else {
turn = TURN_COMPUTER
}
Search WWH ::




Custom Search