Objectives:

  • Use the Keyboard to Control Clips

Using the Keyboard to Move Movie Clips


Now we are going to learn how to use the keyboard to move your clips around the stage. This is a pretty common practice used in most video games. The procedure is a little strange at first. It involves making an invisible button and placing it off the stage. The controls that dictate which keys move your clip are placed on this invisible button.

Here's an example. Use the up, down, left, right buttons to control one ship. Use letters a,s,z,x to control the other.

Space Invaders.

This is how you do it:

  1. Draw your spaceship. Convert it to a movieclip symbol.

  2. Make a small invisible button, as you did in the previous lesson. (Only place a keyframe in the hit state and draw a small rectangle). The rectangle you draw does not need to match the size of your space ship.

  3. Make sure your ship is on the stage.

  4. Right-click on your ship movie clip and open the instance panel.

  5. Type in the name ship.

    Note:You can drag as many "copies" of your movie clips onto the stage as you like. In Flash, copies are called instances. As long as you give each instance a name, you can use ActionScript to program them.

  6. Add a layer named button.

  7. Zoom out on your stage to the 50% level.

  8. While making sure you are still in the button layer, drag your invisible button from the library off to the side of your stage. Be sure your button is not on the stage. It needs to be off the stage.

  9. Right-click on the button, open the Actions Window and paste in this bit of code:

    key control code

  10. Next, add a layer to your scene and name it script.

  11. Double-click the first frame to open the Frame Actions window.

  12. Go into Expert Mode by clicking on the little arrow in the very top right corner of your Frame Actions Window.

    Type: speed = 5;

  13. Test your movie to make sure it works.

  14. Repeat the above steps while keeping everything in separate layers. You can use the same movieclip, but give it a different instance name, say ship2. When you make your second invisible button, you can use this code as an example:

    key control code for ship2

    Notice that the code for letters is a little different from the code for up and down arrows.

    You don't need to re-do the step where you type speed = 5. You can, however, change the amount of speed to a larger or smaller number.



    << Back to Skyline