Game Development Reference
In-Depth Information
xSpacing is a value calculated by splitting the screen size into as many parts as we have ships.
We will use this in a second. First, we create a for:next loop to cycle through the ships. Then,
we create a temporary ship ( tempShip ) that we can work with.
for (ctr = 0; ctr < tShips; ctr++) {
tempShip = new Ship();
OK, now here is the big piece of code. To calculate the x value for a ship, we multiply the counter
(ctr+1) by xSpacing to find out what part of the screen it belongs in. We then subtract
xSpacing/2 to find the center of the area. After that, we again subtract half the width of a Ship so
that that ship itself will be centered in the area (recall that bitmaps are loaded starting at the
upper-left corner, so to find the center we need to subtract half the width).
tempShip.x = ((xSpacing * (ctr+1)) - xSpacing/2) - (tempShip.width/2);
Now, we will clean up by setting the y value to specify the height of the ship ( shipYPadding
pixels ) and put it into our shipArray array, and then add the ship the screen (see Figure 5-2).
Recall that shipYPadding is the number of pixels between the bottom of the screen and the
bottom of the ships. This is a constant setting, but there is no reason why the game could not be
updated to increase this value at each level. This would increase the difficulty, as the higher the
ships are up on the screen, the harder they will be to defend from the Enemy planes.
tempShip.y = gameHeight-tempShip.height- shipYPadding;
shipArray.push(tempShip);
this.addChild(tempShip);
}
}
Figure 5-2. Ship placement with 3, 2, and 1 ships left in the fleet
Handling newLevel events
Finally, we are almost done with newLevel() . The only thing left to do is to report the level ,
shots , and ships values back to ScoreBoard by dispatching events to update each one.
dispatchEvent(new CustomEventLevelScreenUpdate(
CustomEventLevelScreenUpdate.UPDATE_TEXT,String(level)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT, Main.SCORE_BOARD_SHOTS,String(shots)));
dispatchEvent(new CustomEventScoreBoardUpdate(
CustomEventScoreBoardUpdate.UPDATE_TEXT, Main.SCORE_BOARD_SHIPS,String(ships)));
}
Search WWH ::




Custom Search