A Touch of Class

As you can tell by the horribly cheesy name of this post, this one is about classes in AS3.

For me the move to AS3 also meant the move to doing everything in class files as opposed to writing it in the on a frame of the timeline.

This was very confusing and difficult for me as I was not only learning the new syntax of AS3 but also was thrown head first into event life cycle, display list issues and other things that come along with writing classes and class packages. Stuff that I had never dealt with before (or at least didn’t know I was dealing with it).

This will be the first in a series of posts on classes in Flash and I will be learning as much as you do I’m sure. So here we go.

This is the basic syntax for creating a class in AS3:

The package keyword tells Flash were to find this particular class. In this case, the actual class file should be found in the same folder as the FLA that is referencing it. If the line read package foobar Flash would look for the file in the same directory as the FLA in a folder called foobar.

When you get into larger projects and start using 3rd party class packages you’ll run into package names such as com.ryanwelcher this is because each class package must be unique and so the convention is to use the domain name of the developer in reverse order as domain names must also be unique. This avoids conflicts that would require renaming of possibly hundreds of class files. The package statement is followed by curly braces.

Inside the curly braces of the package statement are the import statements. Anything that you want to use in your class have their own class files that you must import before you can use them. This is a big change for anyone used to doing everything in the Flash IDE on the timeline as most of the standard classes were available without needing to import them. The more classes you write, the better you’ll get at remembering the classes you need to import and where to find them.

TIP: You can use the built in flash help menu to source the class package you need. Just highlight the method and click the help icon and it should take you to the correct place.

We have imported the Sprite class in our example.

The next line is a big one as there are a few things going on. This is where you start creating the actual class. The first word public is called an access modifier and I will touch on that more in a later post but for now you will always use public when declaring a class.

Following public isĀ  class and MyClass. Basically you are saying that you are creating a public class named MyClass. The name is up to you but you must name the class with using camel case, and the filename you give the .as file as must match the the class name exactly (it is case-sensitive).

Next you are telling Flash that your new class extends Sprite.

This is an example of inheritance which means that your new class will inherit all of the methods, variables etc of the class it inherits from.

For example the Sprite class has an x property and since we are inheriting or extending that class into ours – our MyClass now has an x property along with all the other items that comes along with the Sprite class.

You may have asked why we used the Sprite class. That is because every class you write in AS3 must inherit from either the Sprite or MovieClip class. There is a detailed explanation there but suffice to say that both of those classes inherit from the DisplayObject class.

The main difference between the Sprite and MovieClip classes is that MovieClip has a timeline and will increase file size in your SWF. So my personal rule of thumb is to use Sprite with I don’t needĀ  a timeline and MovieClip when I do.

Next we add the curly braces after Sprite

Inside of the curly braces is the Constructor this is a special function and is how the class is created when you compile your code. Again we have the access modifier public , the word function and the name of the class followed by curved braces. Pay special attention to make sure your function name and class name are exactly the same and you’ve written your first class! This class will do absolutely nothing because all we’ve done is create the framework. Adding a trace command inside the function will allow you to see that the class works

Ok now what? First save your class file making sure to name the file MyClass.as then using the Flash IDE create a new AS3 project and save it in the same directory as your MyClass.as file (you can call it whatever you like), Next in the properties panel of your Flash project locate the Class box and type the name of the class without the extension. See screenshot.

Now publish your SWF and you should see “My First Class” in the output window.

Obviously this example is pretty useless but hopefully it helped with a little bit of insight on class creation.

Leave a Reply

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