AS3 - learning the ropes
I’m getting stuck into AS3 to build my new site and it’s not as straight forward as I first thought. Almost everything I have learnt and taken for granted I now have to revisit and rebuild. Generally however, I’m pleased with all the new coding procedures and class re-writes that adobe have given in the updated language.
Here are three interesting ‘features’ I discovered yesturday:
Stage
Stage can only be accessed though a visible displayObject.
I discoved this when tying to build an AS3 stageManager class. I didn’t want to make the class extend a sprite and have to add it to the displayList. So I decided to pass it a displayList object as a reference that it uses to measure the stage size.
AS3 Singleton
You can’t make the constructor private! So the old singleton is broken. I googled this problem and found that many people pass the constructor a private variable or method so it can only be successfully implemented internally. Problem is that the class looks a bit messy. Then I discovered another implementation by Daniel Hai on http://www.onflex.org/code/
package
{
public class Singleton
{
private static var instance:Singleton = new Singleton();
public function Singleton()
{
if( instance ) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
}
public static function getInstance():Singleton
{
return instance;
}
}
}
E4X and namespace
I was attempting to read a feed from here: http://kuler.adobe.com/kuler/API/rss/get.cfm?listtype=rating
If you look closly the swatches are on nodes under a kuler namespace:
…
<kuler:themeItem>
…
<kuler:themeSwatches>
<kuler:swatch>
<kuler:swatchHexColor>468966</kuler:swatchHexColor>
</kuler:swatch>
</kuler:themeSwatches>
…
</kuler:themeItem>
…
I haven’t come accross this notation before. In AS2 I could just barge in and parse the nodes as “kuler:themeID”. So this led me to discover the namespace class. Whereby, if I understand it correctly, you target the nodes within a specific namespace. Combined with the beauty of E4X, my query became:
var kuler_ns:Namespace = xml.namespace("kuler");
var hexList:XMLList = xml..kuler_ns::swatchHexColor.text();
Isn’t that pretty.
Amazing. I’ll reccommend my friends to visit you. How long did you write it??