4 Replies Latest reply on Mar 26, 2007 2:42 AM by rlync04

    Using keystokes rather than mouse

      Hey folks,
      I'm a bit of a novice at this lingo so if anyone can help out there it'd be much appreciated. I'm creating a sliding tile puzzle game, and the lingo that is operational allows the tiles to move on mouse clicks. However I want to alter the code so that the individual tiles (Sprites 1-9) move on their relevant keys i.e. 1-9. I'm using sendAllSprites to move the tiles on the mouse but I'm stuck with the keystroke command. I guess I'm supposed to put individual code on each of the sprites with the relevant 'keystroke' command????
        • 1. Re: Using keystokes rather than mouse
          Lukewig Level 1

          There are a few different ways you can do it:

          1. Check for #keyDown events
          2. Check, on every #exitframe, what keys are currently pressed using KeyPressed()
          3. Use the 'keyDownScript'

          To respond to Keydown events, you need to remember that #keyDown events are only sent to certain sprite types (editable text etc). If your tiles are bitmaps, they probably wont receive #keyDown. In this case, using a frame script like this might help

          on keyDown (me)
          k = the key
          case(k) of
          "1": doSomething()
          end case

          With the second approach, you would add a behaviour like this onto each of the individual tile sprites:

          on exitframe (me)
          if keyPressed(thekeythatIamInterestedIn) then
          end if

          With the third approach, you would do something like this

          on startMovie
          the keyDownScript = "MyKeyControlScript"

          on MyKeyControlScript ()
          k = the key
          case(k) of
          "1": doSomething()
          end case

          - Luke
          • 2. Re: Using keystokes rather than mouse
            Level 7
            Another trick that's useful if you want to have dual keyboard and mouse
            controls: Set up your mouseUp commands as normal, then in the frame script
            or whatever, you can have a bit that goes like:

            if keyPressed("A") then sprite(X).mouseUp()

            This will cause the sprite(X) to recieve a mouseUp event when you press the
            'A' key, just as if you'd clicked the sprite with the mouse. That way if
            you want a mouse click and a keyboard shortcut to do the same thing, you
            don't have to duplicate code. Just set it up on the mouse and simulate a
            mouseclick when the relevant key is pressed.

            • 3. Re: Using keystokes rather than mouse
              rlync04 Level 1
              Thanks for the pointers Luke. I've tried the various codes and the best I can manage is getting the relevant sprite to move on one keyDown. When I try it for the next letter/sprite nothing happens. I'll give it another go and see what I can come up with...
              • 4. Re: Using keystokes rather than mouse
                rlync04 Level 1
                Thx for the lingo pointer Darrel. Your lingo seems straight forward but after over-coming some handler code errors, the movie plays but nothing happens on the keystroke. I'll speak with my lecturer on the code and see what we can come up with...