Scoring and Multiple-Game Controls


To display each player's score, I created a score movie clip. In it, I created a new dynamic text field and named it number. (See Figure 25.12.) I placed an instance of the score movie clip inside the player1 and player2 movie clips. The dynamic text field gets updated by the bomb movie clip when it calculates that one of the tanks is hit.

Figure 25.12. The dynamic text field contains the value of 0 when the game is started, and it is incremented throughout the game.

graphics/25fig12.gif

Multiple Games

Although we had decided during the storyboarding process that the game would be over after five kills , it really didn't make sense that a tank that had been exploded could keep playing. So, I made the game end after each kill.

First I created a new movie clip with the name newgame. I created a dynamic text field, put "Winner goes here" in it, and named it winner. Then I placed an invisible button and sized it to cover everything. On the invisible button, I put the following code:

 on (press) {     // Switch players      _root.switchPlayers();      // Randomize the wind      _root.randomizeWind();      // Remove this MC      removeMovieClip("_root.thenewgame");  } 

The switchPlayers() function makes the appropriate fire button visible, the randomizeWind() function randomizes the wind speed and direction, and the built-in removeMovieClip() function removes the entire newgame movie clip from the stage.

To attach the new newgame movie clip, I created a new gameOver() function in frame 1 on the main timeline:

 // This function is called by the last frame of the shell blast MC  function gameOver() {     // Whichever tank got blasted, the other player scores      if (player1tank.blast._visible) {         // Player 2 scored!          ++player2.score.number;          attachMovie("newgame", "thenewgame", 5);          thenewgame._x=270;          thenewgame._y=120;          thenewgame.winner="Player 2 Scores!";      } else {         // Player 1 scored!          ++player1.score.number;          attachMovie("newgame", "thenewgame", 5);          thenewgame._x=270;          thenewgame._y=120;          thenewgame.winner="Player 1 Scores!";      }  } 

The function attaches the newgame symbol I'd just created, positions it in the appropriate place on the stage, and updates the dynamic text field to indicate which player scored. To call this function when a tank is hit, in the last frame of the shell blast movie clip I replaced the call to the switchPlayers() function with the following code:

 // Update the score and offer another game  _root.gameOver(); 

It isn't necessary to change the location of the tanks at the end of a game, for two reasons. First, the wind is randomized at the beginning of the next game. Second, the opposite player gets to begin the next game, so he has an opportunity to move his tank to a new location.



Inside Flash
Inside Flash MX (2nd Edition) (Inside (New Riders))
ISBN: 0735712549
EAN: 2147483647
Year: 2005
Pages: 257
Authors: Jody Keating

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net