Reputation: 632
I have a textField that displays some text, then when a timer goes off, the next piece of text is displayed, and so on. Alternatively, the user can press a key to make the next text appear instead of waiting for the timer.
var dispatchString:String;
function dispatchIt():void {
dispatchEvent(new Event(dispatchString));
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,dispatchIt);
function text1():void {
dispatchString = "text2";
addEventListener("text2",text2);
...
dispatchIt(); //when timer goes off
}
function text2(evt:Event):void {
removeEventListener("text2",text2);
dispatchString = "text3";
addEventListener("text3",text3);
...
dispatchIt();
}
I'm trying to simplify the part where I set the dispatchString and the addEventListener by instead calling a function with one parameter that does both. So something like:
function listenFor(s:String):void {
dispatchString = s;
addEventListener(s,this[s]);
}
But that won't work with nested functions. I was thinking there might be another way to pass the function instead and set the dispatchString to the name of that function, but I haven't had much luck. Any other ideas?
Upvotes: 1
Views: 180
Reputation: 5869
Instead of setting the string of the event, why not set the event handler?
I would try the following:
var eventHandler:Function = text1;
function dispatchIt(evt:Event):void {
callLater(eventHandler,[evt]);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,dispatchIt);
function text1(evt:Event):void {
eventHandler = text2;
trace("text1");
...
dispatchIt(evt); //when timer goes off
}
function text2(evt:Event):void {
eventHandler = text3;
trace("text2");
...
dispatchIt(evt);
}
function text3(evt:Event):void {
eventHandler = text1;
trace("text3");
...
dispatchIt(evt);
}
Upvotes: 1