Linked In Profile  RSS Feed
Posted 20 October 2008

In: AS3 | Flash | Tips

3 Comments

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:

Stage=null

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);
}
else
{
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) {
which.removeChildAt(which.numChildren-1);
if (which == null )
break;
}
 }

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);
slider_mc.stopDrag();
}

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.name = “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:
 some_mc.getChildByName("my_mc");

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. :)

Posted 17 October 2008

In: Uncategorized

No Comments

You know when you’re trying really hard to understand something complicated and all of a sudden one day things just click? Folks, it’s happening. The mysteries of AS3 are becoming clearer and I’m finally having a moment or two of lucidity! And I have to say, I’m also beginning to understand all the hype about AS3 and OOP.

There’s still a long road ahead of me. But the fact that I now understand what the ridiculous phrase “implicit coercion” means is a big step for me! (For those of you who still don’t know, it means you’re trying to pass one type of variable when it’s expecting a different type - for example, you’re passing a String when it’s expecting a Number. Why they couldn’t simply say: ‘wrong variable type’ is beyond me).

I will start posting some of my findings soon but I’m so excited that I have now successfully published a file without 30 errors popping up, that I couldn’t contain myself. :)