|
Want to thank me for making this book available for free? Just buy Special Edition Using Macromedia Director MX
Advanced Lingo For Games
| |
| -- get character code
if ascii >= chartonum("a") and ascii <= chartonum("z") then -- is it lowercase?
c = numtochar(ascii-32) -- convert to uppercase
end if
return c
end
When the player clicks on a letter to try it out, the message is passed on to the frame behavior via the "on tryLetter" handler. This handler first makes sure that a letter has been chosen{6}, and not a space or anything else. The handler then determines if the letter is contained in the phrase anywhere{7}. If not, the "pHangmanNum" property is incremented. Either way, the letter is added to "pLetters". This handler then calls both "on showWorkspace" and "on showLetters" to update all the text on the screen. The new "letters" text member will be missing the letter chosen, because it was added to "pLetters". For the same reason, the new "workspace" text member shows that letter in all the positions, if any, in which it appears in the phrase. The "on tryLetter" handler is also responsible for seeing if the "pWorkspace" property is identical to the "pPhrase" property. If they are, then the player has successfully guessed the phrase and this portion of the game is over. You might also want to take note of the first code line of the "on tryLetter" handler. This checks to see if the "pGameInProgress" property is TRUE before allowing the letter to be considered. The only reason that the "pGameInProgess" property would be FALSE is if the current phrase on the screen has been solved, or if the hangman has been completed, but the player has not yet clicked the Next Phrase button.
-- message sent by text sprite click
-- allows the user to guess a letter
on tryLetter me, letter
-- make sure this is still allowed
if not pGameInProgress then exit
-- make sure this is a letter and not a space or something
ascii = chartonum(letter)
if ascii < 65 or ascii > 90 then exit{6}
-- if the letter is not in the phrase, then advance hangman graphic
if not (pPhrase contains letter) then {7}
if pWrongLetterSound <> "" then puppetSound pWrongLetterSound
pHangmanNum = pHangmanNum + 1
showHangman(me)
else
if pRightLetterSound <> "" then puppetSound pRightLetterSound
end if
-- add to list of letters guessed
add pLetters, letter
-- update screen
showWorkspace(me)
showLetters(me)
--see if the phrase is complete
if pWorkspace = pPhrase then
if pLiveSound <> "" then puppetSound pLiveSound
member("message").text = "Game Over. You Got It. Try Another."
pGameInProgress = FALSE -- do not allow more guessing
end if
end
The behavior also contains the usually frame loop code in the on exitFrame handler. -- frame loop on exitFrame go to the frame end The Letters BehaviorThe sprite with the "letters" text member must have a short behavior attached to it to pass along letter choices. When the player clicks on the sprite, this behavior figures out which letter has been clicked and passes it along to the frame behavior.
on mouseUp me
-- get the character position clicked on
charPos = locToCharPos(sprite(me.spriteNum).member,[cc]
(the clickLoc)-sprite(me.spriteNum).loc)
-- see which letter that is
letter = sprite(me.spriteNum).member.text.char[charPos]
-- send it along to the frame behavior
sendSprite(0,#tryLetter,letter)
end
The New Phrase Button BehaviorTo allow the player to advance to the next phrase, there must be a button for the user to click. In the example movie on the CD-ROM, this button is always available. The player can advance to the next phrase at any time, either when they are done with a phrase, or if they simply want to skip it. The example movie also places this script in a button cast member to make the Cast easier to view. However, you can place it on a screen element as a separate behavior. on mouseUp sendSprite(0,#setUpPhrase) end | |