This doesn’t make any sense to my feeble brain, and I’ve reached my googlefu limit.
I have a bunch of prototype methods that I use to speed development up, and most of them are attached to DisplayObject or InterActiveObject. Here’s an example of one such prototype:
DisplayObject.prototype.$click = function(clicked:Function, released:Function = null):void{
this.addEventListener(MouseEvent.MOUSE_DOWN, function():void {
clicked();
});
if(released != null){
this.addEventListener(MouseEvent.MOUSE_UP, function():void {
released();
});
}
}
If I call any movieClipInstance.$click(someFunction), it binds correctly. However, if I attempt to do that on a TextField instance, it gives me the following compile-time error.
Call to a possibly undefined method $click through a reference with static type flash.text:TextField.
According to the reference, TextField inherits DisplayObject, but just to be sure I redefined my prototype explicitly for TextFields. Still the same error. 🙁
You can always extend TextField and add the methods there. In fact, you should consider doing this for all the other objects as well: Extend MovieClip, Sprite, or whatever other DisplayObject you want to add the $click method to. Then let all your other objects inherit ClickMovie, ClickSprite etc. instead of the generic classes. It should not be a lot more code to write, but your application will be considerably faster and type safe.
You should use prototype only in cases where you have to add or override methods or properties dynamically at runtime.
An in-depth explanation on how inheritance works in AS3 can be found here.