So – I found, from a co-worker, a novel way to handle a lot of EventDispatcher events from a class instance talking back to the main timeline, using the Delegate class. I’m not sure if it’s kosher, but it seems to work…unless I goof up the syntax when I set it up.
Here’s the code that works:
itemsDelegate = Delegate.create(this, itemsChange);
[COLOR=Red]**function itemsChange(evt_obj:Object):Void**[/COLOR]
{
switch(evt_obj.type)
{
case "answer" :
trace("Answer selected : " + evt_obj.answer);
break;
default :
// do nothing for default case
break;
}
}
queItemClip.addEventListener("answer", itemsDelegate);
I can fire off, from the that class instance (queItemClip) as many event types as I need and use the switch case to handle them all. Just a variation of writing the event listeners, I guess.
However, I can’t get it to work if I write the code block this way:
itemsDelegate = Delegate.create(this, itemsChange);
[COLOR=Red]**this.itemsChange = function(evt_obj:Object):Void**[/COLOR]
{
switch(evt_obj.type)
{
case "answer" :
trace("Answer selected : " + evt_obj.answer);
break;
default :
// do nothing for default case
break;
}
}
queItemClip.addEventListener("answer", itemsDelegate);
Why does the named function variant work and the anonymous function fail? Keep in mind, I’m writing the anonymous function variant differently than…
var itemsChange:Function = function(evt_obj:Object):Void {}
Is it a scope issue?
Thanks,
IronChefMorimoto