Razorberry’s Adobe Flash Blog

October 27th, 2006

AS3 learning process: Stuff that wasn’t obvious #1

Posted by Ash in Actionscript
Actionscript

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

Macromedia XML News Aggregator