Note About This Book: Advanced Lingo For Games was written by Gary Rosenzweig in 2000 for users of Macromedia Director 7. It is presented here for free on an as-is basis, with no updating. Most of the information and code here can be used in the most recent version of Director. The book has been reproduced from the final editing files archived in 2000, and not the final proof galleys. So some minor differences between this version and the printed version my exist. The entire contents of this book are Copyright 2000, Gary Rosenzweig. No part may be reproduced or copied without written permission. The text here is provided for individual use only.
Want to thank me for making this book available for free? Just buy Special Edition Using Macromedia Director MX and we'll call it even!

Advanced Lingo For Games
by Gary Rosenzweig


Chapter 14 Section 4

-- 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

The on mouseUp handler takes a mouse click from the user and tries to reposition the text cursor to the letter underneath the mouse cursor.

First, the on mouseUp handler checks to see if the mouse cursor is even over the text sprite. If it is, the handler uses pointToChar to determine which character the cursor is over and then the handler sets the text cursor position to that character.

If this character is not a letter of the alphabet, then the cursor position is set to 0, which tells the "on showCursor" handler not to display the cursor at all. The same is true if the mouse cursor is not even over the text member at all.


-- when the player clicks on the text, select that character
on mouseUp me
  if rollover(pEncodedTextSprite) then -- clicked on text
    -- get character clicked
    pCursorPos = pointToChar(sprite pEncodedTextSprite, the mouseLoc)
    -- make sure it is not a space or punctuation
    if (" ,.-" contains pRealPhrase.char[pCursorPos]) then
      pCursorPos = 0
    end if
  else -- clicked outside of text
    pCursorPos = 0
  end if
  showCursor(me)
end

The "on showCursor" handler is responsible for setting the position of the text cursor sprite according to "pCursorPos". It uses charpostoloc to determine the pixel position of the character in the text member, and then adds the location of the sprite to that to determine the position on the Stage.

If "pCursorPos" is 0, then the text cursor is simply moved off the Stage completely, making it invisible to the player.


on showCursor me
  if pCursorPos <> 0 then
    -- select character
    sprite(pCursorSprite).loc =[cc]
       charPosToLoc(sprite(pEncodedTextSprite).member, pCursorPos) +[cc]
       sprite(3).loc
  else
    -- remove cursor from screen
    sprite(pCursorSprite).loc = point(-100,-100)
  end if
end

The on keyUp handler performs two tasks in this game. First, it determines if the player has pressed an arrow key and moves the text cursor appropriately. Second, it accepts letter characters and applies those to the solution that the player is trying to piece together.

In the case of the arrow keys, the left- and right arrow keys are distinguished by their keyCode.{7} Then, the "d" variable is set to the amount that the cursor should move. The cursor is moved this amount, and then a check is performed to see if the cursor is over a valid letter{8}. If it isn't, the cursor continues to move in that direction. This prevents the player from selecting a space or punctuation. The handler also prevents the text cursor from pointing to a character beyond the end or before the beginning of the text.


-- accept keystrokes to make letter guess and to move the cursor
on keyUp me
  -- arrow keys
  if the keyCode = 123 or the keyCode = 124 then
    -- assign the character movement amount to d
    if the keyCode = 123 then d = -1{7}
    else if the keyCode = 124 then d = 1
    
    -- keep moving the cursor until a valid character is selected
    repeat while TRUE
      pCursorPos = pCursorPos + d -- move cursor
      -- wrap from end to beginning or beginning to end
      if pCursorPos > pRealPhrase.length then pCursorPos = 1
      else if pCursorPos < 1 then pCursorPos = pRealPhrase.length
      -- if not punctuation, then cursor is done moving
      if not (" ,.-" contains pRealPhrase.char[pCursorPos]) then exit repeat{8}
    end repeat
    showCursor(me)

The next part of the on keyUp handler takes a letter and places it into the solution. First, it converts the letter to uppercase. Then, it checks to see if the letter is really a letter, not some other key. Next, it searches through the entire phrase to find every place where the same letter should be used and replaced by the letter in the "pSolvedPhrase" property.{9}

This means that if the player wants to replace the letter "G" with an "A," this game automatically does that throughout the phrase. A pencil-and-paper game requires that the user do this manually.

At this point, there is no check to see whether the player entered the correct letter. That would makes cryptograms very easy.


    -- make a letter guess
  else
    -- get capitalized key
    k = convertToCaps(me,the key)
    
    
    -- see if it is a letter
    ascii = chartonum(k)
    if ascii >= chartonum("A") and ascii <= chartonum("Z") then
      -- see if the cursor is over a valid letter
      if pCursorPos > 0 then
        -- repeat through solved text and replace letter
        letterReplaced = pRealPhrase.char[pCursorPos]
        repeat with i = 1 to pRealPhrase.length{9}
          if pRealPhrase.char[i] = letterReplaced then
            put k into pSolvedPhrase.char[i]
          end if
        end repeat
        -- set text member
        member("solved").text = pSolvedPhrase
      end if
      
      -- check to see if the cryptogram is solved
      if pRealPhrase = pSolvedPhrase then
        phraseSolved(me)
      end if
    end if
  end if
end

When the on keyUp handler is used to replace a letter in the solution, a check is made to see if the solution phrase is the same as the real phrase. If so, that means that the user has solved the cryptogram.

The "on phraseSolved" handler is then called to determine what to do next. If all the phases have been used, then the movie moves on. Otherwise, a new phase is presented and the cursor is reset.


-- if solved, then go to next phrase, or end game
on phraseSolved me
  if pPhraseList.count < 1 then
    go to frame pEndFrame
  else
    setUpPhrase(me)
    pCursorPos = 1
    showCursor(me)
  end if
end

Last, but not least, the on exitFrame handler needs to cause the frame to loop.


-- frame loop
on exitFrame
  go to the frame
end