Macromedia Flash MX 2004 ActionScript Programming Tutorial

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you will learn about:

  • The Key Class
  • Using on(keypress)
  • Using getcode()
  • Using listeners
  • Using components
  • Using the LoadVars() Class
  • Using the XML class to load and manipulate XML
  • Loading an XML document
  • Displaying XML data
  • Manipulating data in an XML object


Classes are pre-defined objects built into ActionScript. Each class is basically a category of related elements. MovieClip is one of Flash’s core classes, and it gives you access to keywords like gotoAndPlay, which are defined for that class. Other built-in classes are Array, Button, Date, Math, String, Color, Mouse, Stage, TextField, Sound, LoadVars, and XML, among many others. You can find all of Flash’s built-in classes in the Actions panel’s Actions toolbox, in the Built-in Classes folder.

You undoubtedly recognize several of the classes just mentioned—we’ve been using them throughout the tutorial. The MovieClip class lets you control a movie clip. The Sound class lets you control a sound. Each of these objects’ properties and methods is defined in its class file.

In Flash MX 2004, you can “extend” these built-in classes, giving the objects additional functionality by adding new “keywords” to their class files (class files have the .as extension). You can also create your own custom classes with the keywords you choose to use. Classes are used to encapsulate code that can be re-used again and again in many different applications. By using several classes together, you can greatly speed up development time of a large application.

If you aren’t building enterprise-sized applications, you don’t necessarily need to worry about classes. Flash’s built-in classes are objects that you’ve already learned to use, and in this section we’ll describe a couple more of them. However, learning to extend and create your own classes can make development of even a small application progress much more quickly.

The Key Class

In this section, we’ll explore the Key class, which lets us assign actions to keyboard key presses. Specifically, we’ll use it to let the user shoot the asteroids in our game before they collide with the ship.

Using the on(keyPress) Event Handler

Various key presses are included as events for the on event handler. If you type on( into the Actions panel, you can scroll through the pop-up menu to see what they are:

But the on() event handler needs to be attached to a button or movie clip. So, if we want our user to be able to press a key, say the space bar, to fire a laser, we need to create some kind of button or movie clip that functions as a “gun”. It can be invisible, or it can be incorporated into the movie graphics. We’re going to go ahead and add a small “gun” graphic to the movie, because later we’re going to give the user the ability to press more keys to move (aim) the gun. We’ve just used a small grey rectangular graphic, which we’ve converted to a movie clip and called “gun_clip”. We’ve placed it at the bottom of the ship’s windshield and called the instance “gun_mc”:

Again, you can use any graphic you’d like.

  1. First, declare a new variable called numShots at the top of your main code (Frame 1 of the main timeline). Set it to 0. This variable will function as a laser shot counter to keep track of all the individual laser movie clips being added to the stage when the user fires.
  2. Because the laser_clip movie clip doesn’t appear on the stage, we’re going to have to use the attachMovie method to duplicate the clip, rather than duplicateMovieClip. Recall that to use attachMovie, the movie clip in the library needs to have a linkage ID. Find your laser movie clip in the library, right-click it, and select Linkage.

In the Linkage Properties dialog, select Export for ActionScript and enter a unique identifier. We’re using “laser_mc”:

Click OK.

  1. On the stage, select the gun_mc movie clip and add the following code to the Actions panel:

on(keyPress “<Space>”) {


this.attachMovie(“laser_mc”, “laser_mc” + _root.numShots, _root.numShots);


By now, this code should look very familiar. We’re using the on(keyPress) event handler to increment our numShots variable and then attach a duplicate laser movie clip to the gun. Each duplicate laser clip will have a number appended to its name that corresponds to its depth in the movie.

We attached the duplicate laser movie clips to the gun, which positions their starting location at the gun’s registration point. This way, the lasers appear to come from the gun.

  1. Save and test the movie. You don’t need to click the play button, since we haven’t added any code to prevent the gun from firing before the play button is clicked. Press the space bar to see the lasers fire across the stage toward the top of the screen:

If you added a sound as we did in the last section, you should also hear a shot each time you fire the gun.

If you want, you can tweak the positioning so the lasers fire from the middle of the gun.

One Comment

  1. zoe

Leave a Reply

Your email address will not be published. Required fields are marked *