How to use Classes in your Project

Many of the examples that I use in my blog come in the form of an Actionscript 3 “Class” file. Without going into too deep into OOP concepts, I will attempt to explain how to implement an as3 class into your project. The following is a class for our example:

package com.frigidfish{
    import flash.display.Sprite;
    public class Monkey extends Sprite{
        private var currentState:String = "";
        public function Monkey(){
            trace('New Monkey Instance Created');          
        public function set jumping(bool:Boolean){
                trace('Monkey is jumping around!');
                currentState = "jumping";
        public function get jumping():Boolean{
            if(currentState == "jumping"){
                return true;
            return false;

The first line is the location of the package. Generally developers will save their classes under a hierarchy that is their website name backwards.. in my case, my game projects are under my business name so all of my actionscript classes are stored under com.frigidfish.

To clarify, lets start by opening flash and selecting File|New, Flash File (Actionscript 3) and click OK. Save the new file to your hard drive somewhere in a folder called “Monkey”.

Now, navigate to the Monkey folder and create a subfolder called “com”. Navigate to the com folder and create another subfolder called “frigidfish”.

Go back to Flash and click File|New. Select “Actionscript File” and choose OK.
You will get a blank page that you can enter code into. Paste the code from above into the blank page and save it in the newly created Monkey/com/frigidfish as

Now go back to the flash file we created first and open the “actions” panel by hitting the F9 key on a PC and on a mac I’m not sure, just click Window|Actions.

In the Actions panel you can start typing in code. The first thing you’ll want to do is import the class that we saved so that you can use it. Enter the following line:

import com.frigidfish.Monkey

Now that the class is available, we can create a new variable instance of it. You can think of a class file as a ‘prototype’. In this case it is a prototype of a Monkey… The cool thing about a class file is that you can make as many instances of it as you’d like.

Ok, so now enter the following line:

var silly:Monkey = new Monkey()

This line creates an instance of Monkey called silly. The Monkey class does not accept any arguments so we just leave the () empty. All classes have what is called a ‘constructor’ function. This is a public function that bears the same name as the class. Anything you put in the constructor function will be executed immediately when the class is instantiated. If the constructor does nothing, it is perfectly legal to leave it out of the class completely, but it is considered bad practice to do so.

In our case, the Monkey class constructor only has one line of code which just traces ‘New Monkey Instance Created’.

Go ahead and test the movie.

If you look in the output panel you will see that not much happens except that you receive the message about the new instance being created. Go ahead and close the test window.

add this line of code next:


If you test the movie now, you will see that in addition to the new instance message, a new line appears that says ‘false’. The default jumping state of the monkey is false.

go ahead and add the next couple of lines:

silly.jumping = true;

If you test the movie now, you should see the following:

New Monkey Instance Created
Monkey is jumping around!

Let’s go through this.

First we created a new instance of Monkey called silly.
Next we asked flash to display the jumping state of silly, which returned false.
Next we set silly’s jumping property to true, and he started jumping.
Last we ask flash to display the current jumping state of silly, which returned true.

So you should be able to see how to get started using classes with Actionscript 3. I also used a couple of getter and setter functions in the class. I will write an tutorial on this but for now a brief overview:

In Actionscript, you can set or get properties using functions. To the person setting the property, they are none the wiser that they are actually using a function.

In our example we simply set the monkey jumping with the statement:

silly.jumping = true;

in your class, if you create a function using the ‘set’ keyword, the function name will be treated as if it were a property. Likewise, the ‘get’ keyword can be used if a developer would like to retrieve the current value or state of a classes ‘property’.

It is important to note that the value passed to the setter function must be of the same data type that is returned from the getter function. Anyhow, I am getting off track but it’s all good information.

I Hope you find this useful in your quest to build more manageable Actionscript 3 projects!

How to dispatch a custom event in Actionscript 3

This is going to be a very simple example of how to dispatch an event. In actionscript 3, you dispatch an event by extending the EventDispatcher class. I’m not going to get into a big complicated explanation right now, but I will show you a couple of simple examples.

One good thing about the Sprite class is that it already extends the EventDispatcher class, so if you are working from your document class, or your class extends Sprite/Movieclip, you can use this anywhere in your class:

// Declare at the top...
public static const CUSTOM_EVENT:String = "customEvent";
// ... Somewhere in your class...
dispatchEvent(new Event(CUSTOM_EVENT));

Let’s say for example you have the following class:

    import flash.display.Sprite;
    public class Cat extends Sprite{
        public static const MEOW:String = "meow";
        public function Cat(){         
        public function meow(){
            dispatchEvent(new Event(MEOW));

Now create an instance from your timeline:

var cat:Cat = new Cat


cat.addEventListener(Cat.MEOW, doSomething);

function doSomething(event:Event){
    trace("The cat is hungry!");

Ok, so this really doesn’t do anything useful, but you can see how you could use this idea to broadcast events from your objects. This is by no means comprehensive, but I have noticed that this simple concept has eluded a lot of people. Later I will write a more robust tutorial showing how to extend the EventDispatcher class and do some cool stuff.