0
votes

I have a Button called 'continueMC' and a MovieClip called 'header'. This is my ActionScript:

continueMC.addEventListener(MouseEvent.CLICK, continueClick);

function continueClick(evt:Event):void {
    fade(header, 0);
}

function fade(evt:MovieClip, fadeTo:Number) {
    var fadeTween:Tween = new Tween(evt, "alpha", Regular.easeIn, evt.alpha, fadeTo, 5, false);
    fadeTween.addEventListener(TweenEvent.MOTION_FINISH, tweenEnd(evt, fadeTo));

}

function tweenEnd(fadeTo:Number) {
    if (fadeTo == 0) {
        trace('here');
        evt.visible=false;
    }
}

I want the MovieClip to become invisible if fadeTo is 0, but only after the fadeTween has finished. When I click 'continueMC', it gives an error saying:

here
TypeError: Error #2007: Parameter listener must be non-null.
    at flash.events::EventDispatcher/addEventListener()
    at _flash_file_fla::MainTimeline/fade()
    at _flash_file_fla::MainTimeline/continueClick()

Why am I getting this error?

1

1 Answers

2
votes

You can't pass tweenEnd(evt, fadeTo) as the listener in addEventListener, instead you can change your tweenEnd method a little so it correctly accepts a TweenEvent, and then check the target of the Tween like so:

continueMC.addEventListener(MouseEvent.CLICK, continueClick);

function continueClick(evt:Event):void 
{
    fade(header, 0);
}

function fade(evt:MovieClip, fadeTo:Number) 
{
    var fadeTween:Tween = new Tween(evt, "alpha", Linear.easeIn, evt.alpha, fadeTo, 5, false);
    fadeTween.addEventListener(TweenEvent.MOTION_FINISH, tweenEnd);
}

function tweenEnd(e:TweenEvent) 
{
    /* 
      e.target is the Tween instance
      e.target.obj is the Object that the tween was effecting, in this case,
      it would be "header"
    */
    if(MovieClip(e.target.obj).alpha == 0) 
    {
        trace('here');
        MovieClip(e.target.obj).visible=false;
    }
}