Join Todd Perkins for an in-depth discussion in this video Preparing a class to be connected to a symbol, part of ActionScript 3.0 in Flash Professional CS5 Essential Training.
- View Offline
Just like a document class, classes can be connected directly to movie clips, so that code can be taken off the Timeline and organized through ActionScript classes. This organization through classes is known as object-oriented programming. It is especially useful when you have many different objects and you want to add the same functionality, like if you wanted to add keyboard control to multiple characters in a game or use that same functionality in a different game. Let's look at how to prepare a class to be connected to a symbol.
I have the file Boarder.as that's a basic class template created by Flash, and then I have some code in the Timeline. This code controls the skateboarder on Stage with the keyboard. So test the movie and use the left and right arrows to move back and forth. Let's go over to Boarder.as. The first thing you need to do is either extend the MovieClip class or the Sprite class. Remember that a sprite is a single-frame movie clip.
Since the skateboarder has multiple frames, it's better to extend the MovieClip class in this case. So, after public class Boarder, type a space and then type extends and then another space and then MovieClip. Note that Flash imports that line of code. That's important. Since I already know we're going to be using the keyboard, I'm going to go to the next line and type import flash.ui.Keyboard;.
It's important to note that these names, like flash.display and flash.ui, are actually package names. Again, we'll get more into package names later on, but for right now, make a mental note that these are actually folder structures. Somewhere on your hard drive, there is a folder called flash, and in there, there's a folder called display, and in there, there's a file called MovieClip.as. Also, in the flash folder is the ui folder, which contains a file called keyboard.
If you want to import all of the files inside of a folder, or in other words all of the classes within a package, you can use the asterisk or wildcard symbol, just like this. I'm going to undo that change though for now. Let's jump back to the Preparing.fla file and go to our ActionScript code. The first thing I'll do is select the import statement and cut it using Command+X or Ctrl+X. I'll move over to Boarder.as and paste the code under the Keyboard import line of code.
Note that the Keyboard is different from KeyboardEvent. Jump back to the FLA and into the code. Select the stage.addEventListener line of code. Cut that code, go back to Boarder.as and add that code to the constructor. Back to the FLA, to the Actions panel, select the keyPressed function. Cut it and delete the extra lines of code and go back to Boarder.as.
Below the constructor method - that's the closed curly brace right below stage.addEventListener - paste the code that you just cut. It may be indented a little bit weird, So just click the Auto format button, and Flash will fix that for you. Now, I'm going to make a simple change to the code to make sure that this code will work for any object we apply this class to. Boarder_mc is going to be irrelevant. We don't want to move boarder_mc inside of the skateboarding movie clip; we want to move the whole movie clip itself - in other words the whole skateboarder.
To do that, I can replace boarder_mc with this. That will move the whole movie clip. A shorter way to write that is to just eliminate this and the dot and just use x += 5 and x -= 5. So let's Save the file, and later on, we'll connect this to a class. The main thing I want you to take away is that in order to prepare a file to be connected to a symbol, you need to extend the sprite or MovieClip Class.
- Understanding the building blocks of ActionScript
- Working with the display list
- Using dot syntax
- Navigating the Timeline
- Creating document classes
- Linking classes to Library objects
- Adding drag/drop functionality to objects
- Creating a slide show
- Loading and running code in an external SWF
- Working with text
- Accessing XML data
- Playing audio and video with ActionScript