Internal Drive Promo Offer
Internal Drive Logo

1.888.709.TECH (8324)   |   CONTACT US   |   CAMP ACCOUNT

iD Tech Bloggers

Properly Referencing Your Main Class in Flash Actionscript

Posted on December 19th, 2008 by KenK

For those of you moving from Actionscript 2 to 3 (with a heavier focus on proper object-oriented principles), you may find that accessing your Main class and Stage isn’t as straight-forward. In this post we’ll highlight organizing your classes to properly reference your main class. First, let’s take a look at our Main class:

package {
    import flash.display.Stage;
    import flash.display.MovieClip;
    public class Main extends MovieClip {
        private static var _instance:Main = null;
        public function Main() {
            _instance = this;
        }
        public static function getInstance():Main { return _instance; }
        public static function getStage():Stage { return getInstance().stage; }
    }
}

First we define a static variable, _instance, to hold a static reference to Main. We are assuming throughout this that we will use Main statically. In other words, there will only be a single instance of our Main class. Next, in our constructor, we use the line _instance = this; to set our static version of Main to the one that is created when the constructor is called.

We will use two methods to get access to Main and the main stage. First, getInstace() is a static function that always returns a copy of _instance. If you want the stage instead, use getStage(). All getStage() does differently from getInstance() is return the stage property of _instance rather than the object itself. Using this format simplifies your code in other classes.

Now let’s say you want to use these functions in other classes to add something to the stage. Let’s pretend we have a Tree class that needs to add itself to the main stage when it is created. You could use this code:

package {
    import flash.display.Sprite;</code>

    public class Tree extends Sprite {
        Main.getStage().addChild(this);

        x = Math.random() * Main.getStage().stageWidth;
        y = Math.random() * Main.getStage().stageWidth;
    }
}

You can see that we can easily get properties of the stage (stageWidth and stageHeight) simply by statically referencing Main. Note that we could have used the following lines instead:

x = Math.random() * Main.getInstance().stage.stageWidth;
y = Math.random() * Main.getInstance().stage.stageWidth;

It really just depends on which example you find more elegant. One additional benefit of using the techniques described here is that you can use the getInstance() to determine if Main has loaded. Simply do:

if (Main.getInstance() != null) { /* some function */ }

Hope that gives you an easy framework for accessing your stage and Main class in AS3!

3 Responses to “Properly Referencing Your Main Class in Flash Actionscript”

  1. Pete Says:

    Ken, This is fun stuff! Cool Flash Tips!

  2. Robert Abramski Says:

    If your in pinch and you want to do it fast and dirty you could also do something like this.

    http://blog.robertabramski.com/2008/03/08/referencing-the-document-class/

    At the time of writing this, this worked, but I see now this technique is better. Good OOP would also say to make your document class a Singleton class as well. Unlike some other ECMA languages like JavaScript, ActionScript doesn’t have private constructors, but there are workarounds.

    http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html

  3. KenK Says:

    I actually wasn’t aware of those two methods of reference, Robert. Thanks for the info and the Singleton reminder!

Leave a Reply

Tags: , , , , ,

All Courses

Do something BIG with our amazing line up of courses.

Computer Camp Courses

View All Courses

Refer-a-Friend

Be an iD Ambassador and earn cool rewards!

  • A credit to your account
  • American Express Gift Card
  • Computer Camp Videos
  • iTunes Gift Card

Get Started

The iD Difference

iD Tech Camps is America's #1 Tech & Computer Camp.

iD Difference image

See the iD Difference

ID Partner logos

Home | Register | Brochure | Summer Camp Locations | Courses & Programs | Why iD? | Blog | Summer Camp Employment | Terms & Conditions | Contact | Site Map

© iD Tech Camps, internalDrive, Inc. All Rights Reserved.

internalDrive, Inc. operates its flagship iD Tech Camps for kids and teens along with the iD Gaming Academy, iD Film Academy and iD Programming Academy. Day camps, overnight Camps, kids camps, children's summer camps and teen summer camps are held at over 60 prestigious universities in the USA and Canada. Make your own game at our game design camps. Learn programming in C++ or Java at our programming camps and robotics camps. Get creative at our web design camps and digital arts camps. Film and edit at our movie camps.