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

Lesson 23. Keyboard Control > Using a case Statement instead of if-then-else

Using a case Statement instead of if-then-else

Now, just as you did in Lesson 22, you will turn this if-then-else structure into a case structure. This will make it easier to add conditions to the keyDown handler for the up and down arrow keys.

1.
Open the movie script and make the changes shown here in bold:

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 - 1       --move the dot 1 pixel left.
   124:
							--If the key code represents the right arrow,
      sprite(2).locH = sprite(2).locH + 1       --move the dot 1 pixel right.
   end case
updateStage                                     --Update the stage to show changes.
end


					  

Recall the case structure you created in Lesson 22. It combines multiple if-then statements into a single structure. This case structure sets up different actions for different values returned by the keyCode, the function that returns the numerical code for the last key pressed. Unlike in Lesson 22, you do not send a parameter to the case structure yourself. The value being tested is the keyCode, which is automatically generated by the system when the user presses a key.

2.
Rewind and play the movie.

Remember that you may need to click the stage to make it active so the arrow keys work. Everything should still work as before.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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