Linked In Profile 
Posted 20 October 2008

As mentioned in my previous post, some things about AS3 are finally beginning to click. Others are still dark and murky, but we are all fortunate to have lots of smart people figuring things out and posting them online for the rest of us. In an effort to further their efforts (and to document some of these things for myself so I’m not hunting for them frantically next time I forget them), here are some simple tips I have found:


I ran into this one today. I was trying to preload a swf into another swf and everything seemed to work fine, except when I tried to simulate download speed so I could preview my work. As soon as I tried simulating the download, AS3 would spit out this cryptic error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
Which translated into plain speak means: Stage = null. The error happens because on the swf I’m loading, I have several references to the stage (such as stage.stageWidth, etc). Well, since nothing has been added to the displayObject yet (I’m assuming), AS3 is kindly pointing out the error of my ways. Problem is, I need to figure out the width of my stage as soon as my movie starts!

Google to the rescue: Mark Ledford explains in more detail what the problem is – but more importantly, here’s the fix (which was actually posted by Steven Sacks – in a somewhat arrogant tone I might add):
if (stage == null)
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
private function onAddedToStage(event:Event = null):void
// init code that requires stage

Clear out an object

I can’t remember where I found this one, but the problem/solution is simple. Say you need to make sure an object has only one child (and for some reason it has something in it already). Simply clear out the object with this:

function clearObjectChildren(which:Object):void {
while (which.numChildren > 0) {
if (which == null )

onReleaseOutside in AS3

One of my gripes about AS3 is that many simple concepts were replaced by ridiculous complexity. Such is the case with the onRelease function that used to exist in AS2. If you’re looking for it in AS3, good luck. It’s gone. So what do we do when we’re dragging something (like a slider) and want to make sure it stops dragging correctly? We add an eventListener to the stage. (random, but it works).

slider_mc.buttonMode = true;
slider_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

function mouseDownHandler(e:MouseEvent):void {
var bounds:Rectangle = new Rectangle(0, 0, 100, 0);
slider_mc.startDrag(false, bounds);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

function mouseUpHandler(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

Using names

Another big difference between AS2 and AS3 that took me some time to figure out was instance names. In AS2, you named everything on the stage (and off) using instance names or variables. In AS3, if your item is physically on the stage (i.e. a movieclip you created on the stage), you can still reference it by instance name, however…if you are creating an object dynamically, you will need to implicitly add a name to it like so: = “my_mc”;

So if you need to access an mc by name (because you’re not sure what number it is in the display line), you can look for it like this:

Makes sense really.

So those are some of the things I’ve learned so far. There are plenty more, but I’m gonna stop while I’m ahead and put some more up later. Hope this helps someone out there. :)

3 Responses to “Quick AS3 Tips”

  1. Holiday Rental Guy Says:

    Thanks for these, clearing out objects was most useful for me, very efficient way to remove those annoying objects when needing to load in a whole new movie clip.

  2. Michael Minor Says:

    A few things you posted are actually overdone, you are adding complexity.

    The rollover handler, you can still just add it directly to the movieclip in AS3… as well as reference anything inside a movieclip as long as it is public (like another movieclip inside), without havign to do the ‘getChildByName’.

    Making the rectangle is an overdone example.

    The reasons they did they took out onReleaseOutside was for performance reason, rather than doing a hit check on everything when the mouse is released, AS3 makes the user keep track of their own programming.

    In the end, AS3 is much quicker than AS1-2 most of the time is because you have to make solid code to create anything complex… Trust me, it’s a good thing in the end.

  3. D Molanphy Says:

    Thanks Michael, would be great if you explained how/why these are overdone – and more importantly, how to correct it.