AS3 learning process: Stuff that wasn’t obvious #1
I finally have the chance to work on a real-world Actionscript 3 project (yeah, its been a long time coming). I realise that this post may be old news (and really pointless) to a lot of you, but I decided to write about things that weren’t immediately obvious to me while learning AS3.
The Loader class doesn’t dispatch events, but Loader.contentLoaderInfo does.
If you take a look at the API documentation for Loader.load(), you’ll notice that the different events dispatched after a load operation are listed, which lead me to think that the loader itself dispatches them. The fact is that you actually need to add your event listeners to Loader.contentLoaderInfo as shown in the example on that page.
In order to use flash.utils.getDefinitionByName(), your class has to actually be compiled into the swf.
Well, duh. Flash player will throw an error if it can’t find your class. This is easily solved by including a reference to the class in your source code somewhere.
private var myClass:Class = com.package.to.MyClass;
Removing a particular item from an array is easier!
array.splice(array.indexOf(obj),1);
Copying attributes of an xml node into an object is.. different.
var o:Object = new Object();
for each (var z:XML in node.@*)
{
o[String(z.name())] = z;
}
You can check if a DisplayObject is actually attached to a display list by checking the stage property.
If the stage property is null, it isn’t attached. This might seem obvious but if you’re trying to find out why something isn’t showing up.. the reason could be that you forgot to actually attach it with addChild().
In Flex Builder’s Actionscript 3 projects, mx classes aren’t immediately available.
You need to add the mx framework.swc to the project under Project->Properties->Flex Build Path->Library Path (tab)->Add SWC.. (button). The file is located at: ${FRAMEWORKS}/libs/framework.swc.
AS3 base types don’t always default to null.
Variables of certain base types such as Boolean and int are no longer ‘undefined’ when declared. This means you can no longer perform checks such as:
if (myBoolean == undefined) doSomething(); since boolean values default to false.
You can find a list of default values on this page in the documentation.
More to come..

December 25th, 2006 at 9:59 am
Hello. Where i can read more about DisplayObject?
January 2nd, 2007 at 9:26 am
Thanks for the AS3 info - just one thing array.splice has not changed - it’s just as easy to delete an element from an array in AS2.
January 2nd, 2007 at 9:27 am
Heh… sorry, my bad. I see what you’re pointing out now - the indexOf method… nice. That will be handy.
February 14th, 2007 at 1:07 pm
But that’s the beauty of AS3 XML. It already *is* in an object, accessible via dot notation. I can’t see many cases where you’d want to clone the XML objects to another class. So maybe they made your special case more weird, but they made normal XML duties 1000x better…
July 25th, 2007 at 11:53 am
XML is no good for data binding. That is the only reason I’ve had so far to convert an XML object to a generic Object object.
Of course, you can always do what Adobe recommends and just convert your XML document to an XMLListCollection but I’ve been having a hard time wrapping my mind around all of the new XML classes. It’s easier for now to just convert the XML object.
January 17th, 2008 at 7:36 am
Useful article to learn the new AS3. Thanks.
November 17th, 2008 at 5:53 pm
geniale : )