Function Override for AS3?

Where I work it’s hard to get everyone on the same page, so it is nice to have some classes written that they can pull from. It certainly makes my life easier when they ask for help because everything is broken. Now I have this great class that is sometimes pulled into an FLA on the TimeLine. By default, the gallery  rotates the image after 4 seconds. What if someone wanted to completely change the rotation or fire other events when the first photo tweens in?

To solve this issue, here is what I did: I am not sure that this is the best way, so if you have a better way, please send it to me. Once my class was finished, I decided I needed to expose part of it to the TimeLine. I created a private variable that was of type Function. Then I used “get” and “set” functions to change that variable.

variable setup:

private var _galleryTweenFunction : Function = defaultImageRotation();

then I added the “get” and “set” functions to my class:

public function set galleryTweenFunction ( newFunction:Function){
_galleryTweenFunction = newFunction;
}


public function get galleryTweenFunction (){
return _galleryTweenFunction;
}

My class is set to call galleryTweenFunction when it’s time to rotate the images. It passes two MovieClips to the function, one being the photo to fade in and the other the photo to fade out. So now the function can be changed by someone on the TimeLine without having to edit the class.

Changing the function on the TimeLine looks something like this using TweenMax:

var myGallery:gallery = new gallery();
myGallery.galleryTweenFunction = function(fadeIn:MovieClip,fadeOut:MovieClip){
TweenMax.to(fadeIn,2,{alpha:1});
TweenMax.to(fadeOut,2,{alpha:0});
}

I also have a get and set for another function that is used to run events when the first photo begins–it transitions in. The class calls this function when the first photo is loaded and ready for animation. It passes the MovieClip the photo is in. So then someone can also just override this function.

myGallery.showFirstImage = function(FirstImage:MovieCliip){
doSomeStuff();
TweenMax.to(FirstImage,1,{alpha:1});
}

This worked great for me. I hope it can help you out as well.