Game Development Reference
In-Depth Information
public function Game() {
addEventListener(Event.ENTER_FRAME, gameLoop);
bg = new BackImage();
addChild(bg);
Next, we move on to the scoreboard. Unfortunately, adding text to the screen programmatically in
AS3 takes a few more lines of code than we would like. This seemingly daunting set of code that
follows really only sets the initial values for the scoreboard (Score: 0, Level: 1, and Misses: 0) and
places those items on the screen. We set all the x values individually across the screen, while the
y value for each item's location is the SCOREBOARD_Y we created a earlier. After we set all the
values for the TextField s, we add them to the display list using addChild(TextField) for each
one. Since we are using the Flash IDE, we could simply drag these items to the stage, but
instantiating them in code makes this easily portable to Flex or even other languages.
scoreLabel.text = "Score:";
levelLabel.text = "Level:";
chancesLabel.text ="Misses:"
scoreText.text ="0";
levelText.text ="1";
chancesText.text ="0";
scoreLabel.y = SCOREBOARD_Y;
levelLabel.y = SCOREBOARD_Y;
chancesLabel.y = SCOREBOARD_Y;
scoreText.y = SCOREBOARD_Y;
levelText.y = SCOREBOARD_Y;
chancesText.y = SCOREBOARD_Y;
scoreLabel.x = 5;
scoreText.x = 50;
chancesLabel.x = 105;
chancesText.x = 155;
levelLabel.x = 205;
levelText.x = 260;
addChild(scoreLabel);
addChild(levelLabel);
addChild(chancesLabel);
addChild(scoreText);
addChild(levelText);
addChild(chancesText);
gameState = STATE_INIT;
}
Initializing the game loop
Since we set the state to STATE_INIT at the end of the constructor, we know that initGame() will
be called the next time the gameLoop() is called. initGame() simply resets all the values to what
they need to be to start playing. We initialize score , level , and chances and update the
levelText.text property to the value of level.toString() (because levelText.text requires a
String ).
Search WWH ::




Custom Search