Activity 2: Keyboard Movement

Intent

Students will create an interface where the user may control a simple object using arrow keys.

Criteria

  • completed project where all arrow keys move an object
  • understand what a keyboard event is
  • Create three if statements from an example
  • Convert the code to pseudocode
  • Understand what the “+=” and “==” operators do

Task

  1. Open Flash. Choose ActionScript 3.0
  2. Draw a circle on the stage. Go Modify, Convert to Symbol. In Properties name the object my_mc.
  3. Press F9 to activate the coding panel.
  4. Add the following code:

    stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_KeyboardDownHandler);

    function fl_KeyboardDownHandler(event:KeyboardEvent):void
    {

                    if (event.keyCode==Keyboard.RIGHT)
    my_mc.x+=10;

     }

  5. Add three more if statements to make the LEFT, UP, and DOWN buttons work. (Hint: there is a pattern)
  6. Add the game you have created to the game page of your website along with the answers to the following questions.
  • How could you alter the code so that the ball moves faster? Slower?
  • What does the x and y stand for?
  • What does += do?
  • What does the symbol == mean?

Extension:

Try the same tutorial but change the object you are moving to a more complex shape or to a small .jpg, gif, or .png image.