Some light reading

Advanced ActionScript 3.0 With Design PatternsJust finished ActionScript 3.0 With Design Patterns, recommended to me by a colleague. It introduces ActionScript 3.0 nicely, while pointing out the best practices and common pitfalls. It covers the more popular and useful design patterns and will change the way you code.


 

However, it had quite a few code typos – most are easy to spot and may be corrected in the next edition anyway – but you will need a sound knowledge of ActionScript already to find this book useful. Despite that, it’s well-structured and highly recommended.

Attaching a Bitmap – AS2 vs AS3

A common task that has changed drastically in ActionScript 3.0 is loading a library image via its linkageID into a BitmapData object and attaching it to a movieclip. In ActionScript 2.0, this can be done like so:

In ActionScript 3.0 movieclips and bitmaps are not attached or created like this – everything is created with the new keyword. Also, a BitmapData object should be dropped into a new Bitmap, which can be added to the stage with the all-important addChild() method.

Since a library bitmap inherits from BitmapData, you now set the class of the bitmap its Linkage Properties in Flash CS3, instantiate it and wrap it in a Bitmap object that you add to the stage:


 

Attaching a bitmap in AS3

Where ‘Butterfly’ refers to the bitmap library item you’ve given the class definition ‘Butterfly’. The Base class is generated automatically. These five lines of code can be shortened to one, but is less readable:

Note that in Flex Builder, unlike Flash CS3, you would embed an image using the embed meta tag, associating it with a variable, instead of its Linkage Properties dialog:

Building SWFs in Flex Builder is a whole different topic – so stay tuned!

XML within a class

I was recently asked about the problem of loading XML from within a class to trigger an arbitrary method. The problem was: the onLoad event triggers on the XML instance, not the class creating it. This could probably be worked around with the Delegate class, but in the past I’ve simply extended the XML class itself, overriding the onLoad handler and adding a callback object that’s passed in (along with some error checking). It’s partnered with an XMLLoader class, the source and simple demo of which you can download here.