Interpreting the User Input


This process is the bigger part of our project. A series of questions are asked, and their answers determine the next step in the process. You can find the checkLetter() function in the GameModel.as file:

 public function checkLetter(sLtr:String) {    trace("GameModel.checkLetter >" + sLtr );    if (isLeft(sLtr)) {       lookForMatch(sLtr);    } else {       gameStatus = "alreadyPicked";    } } 

The isLeft() function looks to see if the selected letter was already chosen, and the lookForMatch() function checks the letter against the word to guess.

Was the Letter Selected Before?

Using a for() loop, the isLeft() function compares the aLettersLeft array to the chosen letter (sChr), one element at a time. If the letter is contained in the array, the letter is deleted from the array, the selectedLetter property is set to the chosen letter, and the function returns true.

 private function isLeft(sChr:String):Boolean {    for (var i:Number = 0; i < aLettersLeft.length; i++) {       if (aLettersLeft[i].toLowerCase() == sChr.toLowerCase()) {          aLettersLeft.splice(i, 1);          selectedLetter = sChr;          return true;       }    }    return false; } 

Note 

In ActionScript 2.0 code, you can specify the data type of the returned value from the function. The data type is declared after the parentheses of the arguments, separated by a colon (:). In the isLeft() function, the data type of the return value is Boolean.

Once the selectedLetter property is set, the "letterPicked" event is broadcasted from the GameModel to any listeners:

 public function set selectedLetter(sChr:String):Void {    _letter = sChr;    dispatchEvent({type: "letterPicked", target: this}); } 

In our code framework, the GameController class is listening for this event, and invokes the onLetterPicked() function, which sets the Y position of the appropriate GameLetter instance 10 pixels below the other GameLetter instances, and changes the color of the text within the GameLetter instance:

 private function onLetterPicked(oEvent:Object):Void {    var sLtr:String = oEvent.target.selectedLetter;    var cgl:GameLetter = mcAlphabet["cgl_" + sLtr];    cgl._y = 10;    cgl.letterColor = 0x666666; } 

Is the Letter Part of the Word?

The lookForMatch() function of the GameModel class compares the selected letter to the value of the selectedItem.label property, one character at a time, and determines the course of action from the result:

 private function lookForMatch(sChr:String):Void {    trace("GameModel.lookForMatch > " +  sChr );    var sWord:String = selectedItem.label;    var sDisplay:String = "";    var bMatch:Boolean = false;    for (var i:Number = 0; i < sWord.length; i++) {       if (sWord.substr(i, 1).toLowerCase() == sChr.toLowerCase()) {          sDisplay += sWord.substr(i, 1);          bMatch = true;       } else {          sDisplay += displayedWord.substr(i, 1);       }    }    displayedWord = sDisplay;    if (bMatch) {       trace("\tfound match");       hitStatus = "hit";       gameStatus =  "matchFound";    } else {       trace("\tdidn't find match...");       for(var i:Number = 0; i < hangManStates.length; i++){          var oItem:Object = hangManStates[i];          if(!oItem.active){             oItem.active = true;             break;          }       }       hangManStates = hangManStates;       hitStatus = "miss";       gameStatus = "matchMissed";    }    checkStatus(); } 

The toLowerCase() method converts the character to lowercase so that the two compared elements would match if they are the same letter, regardless of their case.

If the letter is found in the selectedItem.label value (represented as the sWord variable), the corresponding letter is concatenated (or added) to the value of the sDisplay variable, which is set to an empty string just prior to the for() loop. If there isn't a match, the current letter in the displayed state of the word (displayedWord) is retrieved and added to the sDisplay variable. If a match is found, the variable bMatch is assigned the value of true.

Regardless of the matched letters, the displayedWord property is set to the value of the sDisplay variable. The GameView class will intercept the "wordUpdate" event broadcasted by the displayedWord getter property, and redraw the challenge phrase for the user.

The rest of the lookForMatch() function changes the values of the hitStatus and gameStatus properties of the GameModel class, depending on the value of bMatch. If a match was found, the hitStatus property is set to "hit". The GameView class is listening for changes to the hitStatus property, and plays a sound clip of applause (clap.mp3 in the movie's library). The tStatus field of the GameView class also displays a success message to the player.

However, if the choice doesn't match, a for() loop cycles through the objects stored within the hangManStates property. If the loop finds an object whose active property is false, the active property is set to true and the break action exits the loop. When the hangMan States property is reset to itself, a "hangManUpdate" event is broadcasted from the Game Model. This event is received by the GameView class, which then updates the visibility of the hangman's body parts. The hitStatus property is also set to "miss". This value broadcasts the "hitStatus" event to the GameView class, which, in turn, plays the loss.mp3 sound in the movie's library.

After the proper states have been set in the model, the checkStatus() function of the GameModel is invoked to determine the next state of the game. This function is discussed in the next section.




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