0
votes

Is it possible to add a method to existing (built in) ActionScript Flash class without extending it and making a new class?

For instance I want to create my own fadeOut method and add it to the MovieClip class so that all MovieClips would have that method, and use it like so: ball_mc.fadeOut()

I DO NOT WANT TO EXTEND THE CLASS LIKE THIS:

public class NewMovieClipClass extends MovieClip{ ... }

2

2 Answers

3
votes

you can use prototype in AS3:

MovieClip.prototype.foo=function():void{
    trace("this is a test");
};          
var movieClip:MovieClip = new MovieClip();
movieClip.foo();//this is a test
2
votes

With the advent of Actionscript 3.0 Actionscript is no longer prototypical. This sort of practice is in fact considered poor object oriented design as users of the MovieClip class would suddenly have new methods or unpredictable methods and thus break the design contract. I think the simplest way to accomplish this would be to subclass Sprite or MovieClip adding the methods you require. Is there a reason why you can't do this?