Thursday, November 30, 2006
flash 8 frustrations
This just saved my life.
It's amazing how much 15 lines of code just solved hours of me banging my head against the wall.
For those of you who know anything about flash, the above is a workaround for scope problems when importing XML or LoadVars from within an actionscript class.
<3 whoever wrote that.
comments
Michael said:
Oops, I'm going to tell you a secret now! There's already a built-in class to do that, it's called Delegate. simply:

import mx.utils.Delegate;
var myxml:XML = new XML();
myxml.ignoreWhite = true;
myxml.load("something.xml");
myxml.onLoad = Delegate.create(this, onXMLLoad);
function onXMLLoad():Void {
trace(this); // traces _level0
trace(myxml); // traces xml structure
}
1
Michael said:
oh also, bigspaceship wrote a new class that lets you add your own arguments onto the call, so like delegate.create(this, onxmlload, myparam, "otherparam") it's on labs.bigspaceship.com/blog
2
Michael said:
OH and sry to spamalot but it's useful in other things as well. say you have a movieclip and you HAVE to use the onRelease handler for that mc, as in mc.onRelease = function() (aka you lose the scope to the class) then what i do is define variables in that mc for functions i might need to call from the class like this:
k, tips for you rule my world
mc.setSomeVar = Delegate.create(this, setSomeVar);
mc.doSomething = Delegate.create(this, doSomething);
mc.onRelease = function():Void {
this.setSomeVar(true);
this.doSomething(false);
}
private function doSomething(foo
oolean):Void {
trace(this); // this = your class instance, aka all your members are accesible
}k, tips for you rule my world
3