Saturday, July 08, 2006

ActionScript annoyances

Contrary to what many hardcore developers may think, I do find ActionScript (and its brethren - JavaScript & family) a powerful language. Especially when embedded in Flash, it can do a lot of neat stuff. However, it has some problems for more complex development tasks: visibility (the layers are a pain, and especially in anonymous functions visibility can get tricky), typing, and multithreading (important because some operations are asynchronous which in fact creates a second thread). Finally, the interspersing of code in the FLA file is a readability/maintainability disaster, and thankfully Macromedia addressed this by enabling external class definitions that extend MovieClip or UIObject. The debugger and IntelliSense are both a bit of a joke too...

All of the above are neatly summarized in this: I have an object that has a XML object member. XML.load( file.xml ) is an asynchronous operation and the only way I could find to notify the parent that XML has finished loading the file is by creating an object derived from XML that has a property pointing back to the parent so that it can callback the parent when it has finished. Here it is:

The 'parent' object:

class ActionMenu extends MovieClip {

private var oFile:XMLD;

* constructor
public function ActionMenu( xmlFile:String ){
oFile = new XMLD(this);

* CallbackLoad
* called by the XML object when it finished loading
* the document
public function CallbackLoad(success:Boolean):Void{
// whatever needs to be done
// XMLD implements XML's interface so use it as you
// would use a XML object
trace( "File not loaded!");



The XML-derived object:

* Used to notify Parent (ActionMenu) when the XML document is loaded
class XMLD extends XML{
private var ptrAM:ActionMenu;

public function XMLD( p:ActionMenu ){
ptrAM = p;
this.onLoad = function(success:Boolean){