Skip to main content
vedtam
Known Participant
February 28, 2010
Answered

Timing Tween Calss in AS3?

  • February 28, 2010
  • 1 reply
  • 2077 views

Hi,

Could someone help me out please? I would like to achieve a slide show like instance (for the banner of my site), with at least 5 pictures.I am using the Tween Class for doing this but I can't figure out how to delay them ( for example: after the the first tween stops, how to start the, or jump to the next automatically).

example:

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

var myTween = new Tween(ball_1, "x", Strong.easeInOut, 100,400, 2, true);

myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void
{
    myTween.obj.alpha=0;
}


var myTween2 = new Tween(ball_2, "x", Strong.easeInOut, 100,400, 2, true);

myTween2.addEventListener(TweenEvent.MOTION_FINISH, onFinish2);
function onFinish2(e:TweenEvent):void
{
    myTween2.obj.alpha=0;
}

thanks!

This topic has been closed for replies.
Correct answer Ned Murphy

Fantastic!!! Now it is working perfectly.

I tweaked a bit the parameters of the tween (direction and duration) to get it just right, and I have to say it is wonderful!

But I still have a little problem. I almost wanted to thank for the enormous patient, but than I realized that before I do so I test it in my main flash movie (website), and I noticed that the slideshow overlaps my buttons even if I place it bellow of any important content (I placed it on the bottom layer).

Is this because of he addChild method?How could I fix this??


addChild will bring the object to the top, above anything else on that timeline, so if you have other objects you want to place in top of it, then you will need to do the same for them after you do it for your tweened objects.

1 reply

kglad
Community Expert
Community Expert
February 28, 2010

use the tweenfinish event and the timer class to time tweens or, even easier, get one the 3rd party tween classes (like tweenlite).  they all, as far as i know, include a delay parameter that makes it much easier to time tweens.

vedtam
vedtamAuthor
Known Participant
February 28, 2010

I tried TweenLite but to be honest, being a newbie I better stick to the built in Tween Class. But I still can't get the result I want. I managed to do a sequence of 2 Tweens, but as I add a third  tween the previous twoo won't play any more. This code is working ok, but I can't figure out how to add 2 more tweens and to loop them.

Please help

import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.utils.Timer;
import flash.events.TimerEvent;


//timer created

var timer:Timer = new Timer(4500, 1);
timer.addEventListener(TimerEvent.TIMER, doNextTween);
timer.start();

var myTweenX:Tween = new Tween(image_1, "x", Strong.easeOut, 0, 482, 2, true);

function doNextTween(e:TimerEvent):void{
  var myTweenX2:Tween = new Tween(image_2, "x", Strong.easeOut, 0, 482, 2, true);
  timer.addEventListener(TimerEvent.TIMER, doNextTween);
}

Regards!!

Ned Murphy
Legend
February 28, 2010

Your timer is only set to fire once in that code, so that is probably why it stops at the second tween.  Also, if it was still timing,  it is calling the same function so it would re-Tween the same object.

If you have 4 different objects to tween, then you might try using the same fuinction to tween all of them, and have the timer take care of triggering the last three.  Create an array with the objects stored in it, and have your function target those instances using a index variable that you increment each time the function executes.  So you set index variable to start as 0 and then either let the timer call the function each time for all four or call the function directly for the first and let the timer call it the last 3 times.  Below shows the later scenario...

import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.utils.Timer;
import flash.events.TimerEvent;

 
var currentImg:uint = 0;

var imgArray:Array = new Array(image_1, image_2, image_3, image_4);

var timer:Timer = new Timer(4500, 3);
timer.addEventListener(TimerEvent.TIMER, doNextTween);
timer.start();

function doNextTween(e:TimerEvent=null):void{
    var myTween:Tween = new Tween(imgArray[currentImg], "x", Strong.easeOut, 0, 482, 2, true);

    currentImg += 1;
}

doNextTween(); // start the first tween