Search code examples
actionscript-3flashtween

Tween yoyo is permanent


Original goal was to play animation in reverse on event1, and normally on event2. So I used this code:

/* some imports */
public class Element extends MovieClip {
        private var tween:Tween;

        public function Element() {
            tween = new Tween(this, "y", Regular.easeInOut, -this.height, 0, 0.7, true);
            tween.stop();

            this.addEventListener(event1, reverse);
            this.addEventListener(event2, normal);
        }

        private function reverse(e:Event1) {
            tween.yoyo();
        }

        public function normal(e:Event2) {
            tween.stop();
            tween.rewind();
            tween.start(); 
        }
}

Now I tested it: 1. Event1 occured, animation played normally 2. Event2 occured, animation played in reverse 3. Event1 1 occured, and animation played in reverse

For some odd reason, the yoyo is somehow permanent. I know some workarounds like creating new Tween for each animation, checking if yoyo was already used, etc.

Is this expected behaviour? In adobe API I read about begin and finish props and they both have "This property is set as a parameter when creating a new Tween instance or when calling the Tween.yoyo() method.". Do I have manually reset them?

I don't want to use Tweenmax, tweenlite, or any external library.


Solution

  • The manual says that to make your tween running normally again, call another yoyo(). Unfortunately, the tween itself does not provide any hint on if it's yoyo'd right now, so you'll need to track this.

        private var tween:Tween;
        private var tween_yoyo:Boolean;
        private function reverse(e:Event1) {
            if (!tween_yoyo) tween.yoyo();
            else {
                tween.stop();
                tween.rewind();
                tween.start(); 
            }
            tween_yoyo=true;
        }
    
        public function normal(e:Event2) {
            if (tween_yoyo) tween.yoyo();
            else {
                tween.stop();
                tween.rewind();
                tween.start(); 
            }
            tween_yoyo=false;
        }