Flowers
Flowers

Reputation: 59

move a Flash object forth and back along the X axis with AS3

I am creating a flash animation with a flash object that moves along the X axis back and forth. I would like to move the object for example from position x=10 to position x=100 then stay in position x=100 for 2 seconds and go back with the same motion characteristics to the initial position. I am using Tween Classes, that let me change eases and so on, i am also using the Tween yoyo method. The problem is that I don't know how to make the object stay for some seconds in the second position (before going back to the initial position) and I don't know how to stop the yoyo event. I want the object to go back and forth and then stay in the initial position but with the yoyo it keeps on repeating (i supose that i should not use the yoyo method...). I attach you the code I have so far:

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

var myTween = new Tween(rectangle, "x", Strong.easeInOut, 100,300, 1, true);

myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
myTween.yoyo();
}

Upvotes: 0

Views: 1671

Answers (1)

Mircea
Mircea

Reputation: 925

You should set a timeout in the onFinish method. The code should look like this:

import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;

var myTween = new Tween(rectangle, "x", Strong.easeInOut, 100,300, 1, true);

myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
  var delay:int = 1000;
  setTimeout(delay,reverse_tween); // delay - delay in milliseconds
}
function reverse_tween():void{
  myTween.yoyo();
  myTween.removeEventListener(TweenEvent.MOTION_FINISH, onFinish);
}

Hope it helps

Upvotes: 0

Related Questions