Game Development Reference
In-Depth Information
Variables
Now, we move to the code inside our Game class. The first thing we do is define variables to hold
the player's score (the renamed clicks variable from the first game), chances (the number of
missed balloons), and level .
public class Game extends flash.display.MovieClip{
const STATE_INIT:int = 10;
const STATE_PLAY:int = 20;
const STATE_END_GAME:int = 30;
public var gameState:int = 0;
public var score:int = 0;
public var chances:int = 0;
public var level:Number = 0;
Next, we need to create some variables to hold the objects that we will create while the game is
being played. bg will hold the background image—BackImage()—from the library. enemies is an
array that will hold all the instances of EnemyImage() (the balloons) that we create for the player
to pop. The player is an instance of PlayerImage() from the library.
public var bg:MovieClip;
public var enemies:Array;
public var player:MovieClip;
Next, we will create TextField objects for displaying the score, level, and chances (misses). We
will now refer to this as the scoreboard, even though it is only logically a scoreboard with all the
elements defined individually. We will need two TextField objects for each item we want to
display, a label and place to display the value:
public var scoreLabel:TextField = new TextField();
public var levelLabel:TextField = new TextField();
public var chancesLabel:TextField = new TextField();
public var scoreText:TextField = new TextField();
public var levelText:TextField = new TextField();
public var chancesText:TextField = new TextField();
Finally, we need to create one other new variable; we need to define a y position on the screen
for the scoreboard TextField objects, and we want this to be at the bottom of the screen. Since
the height of the screen is 400, we will set this to 380 so that they will appear just at the bottom of
the screen.
public const SCOREBOARD_Y:Number = 380;
We created this variable because when the y value of the scoreboard changes, all of the
elements move. Using y will save us from editing each line of code that contains the y values for
the scoreboard. The x value always changes per item, so we leave that one alone.
Game constructor
Now, we move onto the constructor for Game . The first thing we do is create an instance of
BackImage and add it to the screen using addChild() . We need not set the (x,y) location, because
it will automatically be placed at (0,0), exactly where we want it.
Search WWH ::




Custom Search