Search code examples
actionscript-3flashtween

ActionScript 3 - Passing parameter to an addEventListener


This is my code:

function switchText(evt, newText:String, time=5) {
    var fadeTextTween:Tween = new Tween(evt, "alpha", Regular.easeIn, evt.alpha, 0, time, false);
    fadeTextTween.addEventListener(TweenEvent.MOTION_FINISH, textTweenEnd);

}

function textTweenEnd(e:TweenEvent) {
    e.target.obj.text=newText;
}

I want to access the newText parameter from within textTweenEnd. How do I pass newText as a parameter to the textTweenEnd function?


Solution

  • You can't pass any additional parameters to motion finish handler, so you need to implement some additional logic to bind tweened object with future text.

    I would recommend to use TweenLite, with onComplete and onCompleteParams properties.

    function switchText(evt:TextField, alpha:Number, newText:String, time:Number = 5):void {
        TweenLite.to(evt, time, {alpha: alpha, onComplete: updateText, onCompleteParams: [evt, newText]});
    }
    
    function updateText(textField: TextField, value:String):void {
        textField.text = value;
    }