• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Lesson 23. Keyboard Control > Adding Key Codes for the Up and Down Arrows

Adding Key Codes for the Up and Down Arrows

Now that your coding is optimized, it's time to add controls for the other two keys. To do this, you need to change only the case structure so it includes the key codes for the up and down arrows, which, as you discovered earlier, are 125 and 126, respectively.

1.
Open the movie script and add the two new key codes to the case structure, as shown here in bold:

global gDrawSpeed

on startMovie
  sprite(2).trails = TRUE             --Turn on trails for the dot (sprite 2).
  sprite(2).constraint = 3            --Constrain the dot (sprite 2) to
                                      --the boundaries of the rectangle (sprite 3).
  gDrawSpeed = sprite(2).width        --Tie the drawing speed to the width of the dot.
end

on keyDown                            --When a key is pressed,
  case (the keyCode) of
    123:                              --if the key code represents the left arrow,
      sprite(2).locH = sprite(2).locH – gDrawSpeed
                                      --move the dot left by
                                      --the number of pixels that the dot is wide.
    124:                              --If the key code represents the right arrow,
      sprite(2).locH = sprite(2).locH + gDrawSpeed
                                      --move the dot right by
                                      --the number of pixels that the dot is wide.
    125:
							--If the key code represents the down arrow,
							sprite(2).locV = sprite(2).locV + gDrawSpeed
							--move the dot down by
							--the number of pixels that the dot is wide.
							126:
							--If the key code represents the up arrow,
							sprite(2).locV = sprite(2).locV – gDrawSpeed
							--move the dot up by
							--the number of pixels that the dot is wide.
  end case
  updateStage                         --Update the stage to show changes.
end


					  

The existing sections for key codes 123 and 124 use the the locH property to move the drawing dot side to side. The sections you just added for key codes 125 (the down arrow) and 126 (the up arrow) use the the locV property to move the dot up and down.

2.
Play the movie and press the arrow keys.

It works! Now all the arrow keys draw as they should.

3.
Save your work.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint