Join Todd Perkins for an in-depth discussion in this video Creating a class using Flash templates, part of ActionScript 3.0 in Flash Professional CS5 Essential Training.
- View Offline
Creating an ActionScript class can be a daunting task. There are many required phrases, and the structure of your files has to be perfect or the code will break completely. To make this process easier, Flash has pre-built templates for creating ActionScript class files. You can see these templates through the welcome screen, or you can go to File > New. I mentioned that this file is a template, but you don't find it under the Templates tab.
You'll have to go to the General tab in the New Document window to find ActionScript 3.0 Class. That's the first file. That's an ActionScript file. If you look at the description, you'll see that it says, Create a new AS file with a .as extension to define an ActionScript 3.0 class. So I'll click OK. Here, because of my ActionScript settings, Flash is asking me which app I'm going to use to write ActionScript code. I can either use Flash Professional or Flash Builder.
For this course, we're only going to be working in a Flash Professional. If you get deep into object- oriented programming and you want a more robust application for writing ActionScript classes, then I recommend getting into Flash Builder. But for now, Flash Professional is fine, and I'll click Don't Show Again, and type the Class name. I'll just call this Example, capital E, and then I'll click OK. So, Flash has created an ActionScript file for me.
It's important to note then the ActionScript file is really just a text file. You can actually use the text editor Notepad to create an ActionScript file. As long as it's plain text - so, not rich text - and as long as it has the .AS extension, it can work in Flash. So let's walk through this code. The first thing inside of here is the package declaration. So it says the word 'package' and then a space. The package identifies where this code resides.
If you want to be really organized, you'll group your different ActionScript class files into folders. And packages allow you to tell Flash how that code is organized. This is something that's more of a deep concept in ActionScript classes, and we'll talk about it later on in this course. The next line of code says public class Example. When you create an ActionScript class, use the word 'public' and then a space and then class, just like as if you're defining a function.
You specify the keyword class to create a class. These words like 'public' and maybe you'll see 'private' sometimes, these words are special words that you only use inside of a class file when defining functions and classes as well. Then we have the name of the class which is Example. Flash added that in for us when we typed Example for the name of our class, and then it says public function Example and constructor code. Let's take a look at the organization of the curly braces.
Notice that the open curly braces start on the same line as the package declaration, the public classic declaration, and the public function declaration. If I click the Auto Format button, then Flash will organize this code how I've been writing it throughout this course, which is with open curly braces on separate lines. So we can see the outer curly braces for package wrap all the code in this file. Then the class code is the first level in.
So that wraps the rest of the code in the file. And finally we have the public function Example. That's the most inner set of curly braces. Each time you create a class, you should have this function called a constructor function. A constructor function has the same name as the class name, which in this case is Example. This should also be the same name of your ActionScript file. So the file should be called Example with a capital E .AS.
As we go forward, we'll get more in practice working with ActionScript classes. The main thing I want you to take away from this is that using the ActionScript class file templates in Flash CS5, you can easily create class files without having to worry about getting everything right.
- 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