Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

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.


Previous Page
Next Page
Index: [A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
Table of Contents
Back Cover
Macromedia Flash 8 Bible
Foreword
Preface
Part I: An Introduction to Flash Web Production
Part II: Mastering the Flash Environment
Part III: Creating Animation and Effects
Part IV: Integrating Media Files with Flash
Part V: Adding Basic Interactivity to Flash Movies
Part VI: Distributing Flash Movies
Part VII: Approaching ActionScript
Part VIII: Applying ActionScript
Chapter 28: Sharing and Loading Assets
Chapter 29: Sending Data In and Out of Flash
Chapter 30: Applying HTML and Text Field Formatting
Chapter 31: Creating a Game in Flash
The Game Plan: Four Phases of Game Design
Building the Project
Scripting the Game
Initializing the Game
Building the Interface
Starting the Game
The User Input
Interpreting the User Input
Checking the Status of the Game
Added Feature: Storing User and Game Information
Summary
Chapter 32: Managing and Troubleshooting Flash Movies
Part IX: Integrating Components and Data-Binding
Part X: Expanding Flash
Part XI: Appendixes
Index
List of Figures
List of Tables
List of Listings
List of Sidebars


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele