0

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 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Have you tested this? MovieClip extends object, which is dynamic, and therefore can have other methods and properties attached to it without the compiler complaining. But have you tried this with Number?
2

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?

9 Comments

+1 agree with your answer, but you it's always possible to play with prototypes
prototype is not related to the 'dynamic' class modifier : help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/…
yes but being able to compile without mc.foo() causing issues is.
I agree with the above respondents. You can do what you ask with prototype, and it's up to you to decide if you should.
@Rudi you can use prototype on any object without any issue
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.