July 23, 2004
A TextField Listener Improvement

Did you ever wonder why, if you add a listener to a TextField you only receive the onChanged and the onScroller events, but not the onSetFocus and onKillFocus events? The problem is, that as the TextField is a listener itself, it cannot broadcast the onSetFocus event because this would result in an endless loop, as you can test yourself:

TextField.prototype.onSetFocus=function(oldFocus:Object):Void{
this.broadcastMessage("onSetFocus",oldFocus);
}

If you add an input field to your stage, publish it and select it, Flash will give you the "256 levels of recursion..." error.

But there is a way to work around this and it's one of the rare times that I've found use for "arguments.caller" and "arguments.callee":

TextField.prototype.onSetFocus=function(oldFocus:Object):Void{
if (arguments.caller!=arguments.callee) this.broadcastMessage("onSetFocus",oldFocus);
}

TextField.prototype.onKillFocus=function(newFocus:Object):Void{
if (arguments.caller!=arguments.callee) this.broadcastMessage("onKillFocus",newFocus);
}

What this does is to check where the onSetFocus call originated from. And only if the function was not called from the textfield itself will it propagate it to all its listeners. So if you include these lines and use addListener on a textfield you will receive all four events: onChanged, onScroller, onSetFocus and onKillFocus.

For AS1 it looks like this:

TextField.prototype.onSetFocus=function(oldFocus){
if (arguments.caller!=arguments.callee) this.broadcastMessage("onSetFocus",oldFocus);
}

TextField.prototype.onKillFocus=function(newFocus){
if (arguments.caller!=arguments.callee) this.broadcastMessage("onKillFocus",newFocus);
}

Posted at July 23, 2004 09:17 PM | Further reading
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?



Site Search

Google
quasimondo.com
incubator.quasimondo.com
lectures.quasimondo.com
Most Visited Entries
Experiments
Lectures
Contact
Backlog
In Love with
Powered by