Checking the Status of the Game


The last step in our process is to check the status of the game. More specifically, is the word complete, is the hangman completely drawn, and/or is the game complete? The checkStatus() function is defined in the GameModel class and asks all these questions. In the following sections, we discuss questions and conditions that exist in the game and the programming logic behind their answers and actions.

Is the Word Complete?

To check whether the word is complete, we check for any remaining ? characters in the letter text fields. We use a for() loop (with the value of the displayedWord.length as the condition to exit the loop). Note how the Boolean variable bAllMatched is assigned the value of true if the word is complete and of false if the word is not complete.

 var bAllMatched:Boolean = true; for (var i:Number = 0; i <= displayedWord.length; i++) {    if (displayedWord.substr(i, 1) == "?") {       bAllMatched = false;       break;    } } 

Is the Hangman Complete?

We also need to check the status of the hangman: If all of its parts are visible (that is, active), the round is lost. The bAlive variable determines whether or not the player is still "alive" during the current round:

 var bAlive:Boolean = false; for(var i:Number = 0; i < hangManStates.length; i++){    var oItem:Object = hangManStates[i];    if(!oItem.active){       bAlive = true;       break;    } } 

If one of the hangManState objects has an active value of false, then the Hangman display still has remaining parts to display, and the player is still in the round. As such, the bAlive variable is set to true. Otherwise, the bAlive variables remains false.

Are there More Words to Guess?

After the hangManStates property has been checked, we need to do the same type of check for active words in the game. In others, is it the end of a round, or the end of the game? Has the player played the game long enough to exhaust all of the challenge phrases?

 var bWordsLeft:Boolean = false; for(var i:Number = 0; i < wordList.length; i++){    var oItem:Object = wordList[i];    if(oItem.active){       bWordsLeft = true;       break;    } } 

How do You End the Round?

If all of the characters have been successfully matched by the player, the gameStatus property is set to "wonRound". All gameStatus properties are String values that are looked up in another GameModel property named labels. Therefore, if the gameStatus property is set to "wonRound", then that index value is looked up in the labels property, and returns the value "You won this round!." Whenever the gameStatus property is updated, the GameView class (as a listener) sets the tStatus field to that value, displaying the text to the player.

Note 

All status messages are declared in the init() handler of the GameModel class. Also, the labels property is actually an associative array, which enables you to use a String value as the key to each element in the array. So, instead of using labels[0] to look up a value in the array, you pass a String value as the key, such as labels["wonRound"].

As we continue to break down the GameModel class's checkStatus() function, you can see that gameStatus is set to "wonRound" if bAllMatched is set to true:

 var bReset:Boolean = false; if (bAllMatched) {    gameStatus =  "wonRound";    win++;    bReset = true;    gameState = "win"; } else if (!bAlive) {    gameStatus = "lostRound";    loss++;    bReset = true;    gameState = "lost";    displayedWord = selectedItem.label; } 

The win property of the GameModel class is also incremented if the player successfully matched all of the characters in the challenge phrase, and the bReset value is set to true. When the win property is updated, the tWon instance in the GameView class updates the current win score.

Note 

If bReset remains false, then the GameModel knows that there are remaining words — thus game rounds — to play.

When the gameState property is set to "win", the GameView and GameController classes do not respond with any particular action — you could further enhance this game by having the hangman character animate when the player wins the round (that is, provide a bigger pay-off to the player for winning the round). If bAllMatched and bAlive are false, the player has lost the current round. The gameStatus property is set to "lostRound", the loss value is incremented, bReset is set to true, and the gameState property is set to "lost". The "lost" value tells the GameView class to run the onLost() function, which we describe in the next section.

Removing the Hangman

The onLost() function of the GameView class is executed when the gameState property change is broadcasted from the GameModel class to the GameView class.

 private function onLost():Void {    var mc:MovieClip = attachMovie("beamClip", "mcBeam", image from book       getNextHighestDepth(), {_x:242, _y:0, targetClip: mcHangMan}); } 

This function attaches the beamClip symbol from the library to the Stage, with an instance name mcBeam, above the alien hangman instance (mcHangMan). A variable on the mcBeam instance named targetClip is set equal to the mcHangMan instance. This variable is used by yet another attachMovie() method, found inside of the beamClip symbol.

In the Library panel, open the View Assets ð beamClip symbol. On frame 7 of the actions layer on this symbol's timeline, you'll find the following code:

 var fader:MovieClip = this._parent.attachMovie("BlurFader", "cfb", image from book    this._parent.getNextHighestDepth(), {dir: "out", duration: 0.5, image from book    target: this.targetClip}); 

Here, the BlurFader symbol, linked in the Library panel, is dynamically attached to the GameView instance (which is the parent timeline of the mcBeam instance added earlier). The BlurFader component, which we used in Chapter 20, "Making Your First Flash 8 Project," uses three parameters: dir, duration, and target (or _targetInstanceName). In this attachMovie() method, we pass these parameters and their values. The fader instance is then set up to make the alien hangman (mcHangMan, as set by the this.targetClip parameter) fade and blur out from the stage.

Reset the Game or Just the Round?

If all of the challenge phrases have been played (that is, bWordsLeft is false) and it's the end of the current round, the game is over.

 if (!bWordsLeft && bReset) {    gameStatus = "gameOver";    gameState = "gameover"; } else if (bReset) {    gameState = "roundover"; } 

As such, the gameStatus property of the GameModel class is set to "gameOver", which leads to the display of the text, "Thanks for playing!," in the tStatus instance of the GameView class. The gameState is also set to "gameover". In this version of the game, nothing happens to the GameView or GameController classes when the game is over — the player simply sees the message and is prohibited from playing further rounds.

If there are still challenge phrases left to play and the round is over, the gameState property is set to "roundover", which is detected by the GameController class. There, the nextRound() function is invoked three seconds after the event is received from the GameModel. (You can see the setInterval() function used within the onGameState() function of the GameController class.) The nextRound() function calls the newRound() function on the GameModel:

 private function nextRound():Void {    clearInterval(nDelayID);    model.newRound(); } 

The nextRound() function is delayed by three seconds so that the previous gameStatus property change has time to display the current game status to the player in the tStatus field. If the nextRound() function was not delayed, the user would jump right into the next round, missing any updated display of text.




Macromedia Flash 8 Bible
Macromedia Flash8 Bible
ISBN: 0471746762
EAN: 2147483647
Year: 2006
Pages: 395

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