| online portfolio of Ursula Wing
CONTINUOUS ACTIONSCRIPT TWEENS | AS2

I wrote this tween sequencing class specifically with ad banners in mind, in the pursuit of a simple & lightweight tool to execute a series of actionscript tweens on a single keyframe with somewhat more flexibility than that offered by the continueTo() and onMotionFinished() methods. Place this file in the same directory as your .fla (or alter it to suit your package/class heirarchy preferences) and at the cost of about 1K of functions and 3-5K of AS Classes (will vary depending on how many different easing types you use), you have a set of functions which will do exactly that.

DOWNLOAD SRC

Usage / Parameters

To create a new object:
[ unique name ] = new sweetTween( [ number of loops ] , [ delay between loops ] );

To add one or more tweens to the object:
.addTween( [ movieclip], [ property ], [ start value ], [ end value ], [ start time in seconds ], [ duration in seconds ], [ easing ])

To commence your Tween Sequence. Can be called after the Tween is played.
.start()

To stop the sequence, or remove the objects created by the sequence when no longer needed.
.remove()

Here is a detailed explanation of easing classes & a quick reference:

Classes: Types:
Strong
Back
Elastic
Regular
Bounce
None
easeIn
easeOut
easeInOut
easeNone

Notes:
- Provided you don't attempt to tween the same attribute of the same movieclip with 2 conflicting addTweens(), start times can be out of order and durations can overlap each other your sequence.

EXAMPLE: Functions placed in Scene 1, Actions layer, Frame 1;
NUM

/****************************************
INCLUDES
****************************************/

import sweetTween;

/****************************************
VARS
****************************************/

var ceci:MovieClip;
var bg:MovieClip;
var loop:MovieClip;
var stop_btn:MovieClip;

var ceciSequence:Object;
var loopText:Object;

var no_easing = mx.transitions.easing.None.easeNone;
var bounceOut= mx.transitions.easing.Bounce.easeOut;
var easeOut= mx.transitions.easing.Regular.easeOut;
var elasticOut = mx.transitions.easing.Elastic.easeOut;

/****************************************
ANIMATION FUNCTIONS
****************************************/


ceciSequence = new sweetTween(1, 0);

ceciSequence.addTween(ceci, "_x", -100, 200, 0, 2, bounceOut);
ceciSequence.addTween(ceci, "_alpha", 0, 90, 0, .5, no_easing);
ceciSequence.addTween(ceci, "_xscale", ceci._xscale, ceci._xscale*5, 2.25, 1, easeOut);
ceciSequence.addTween(ceci, "_yscale", ceci._yscale, ceci._yscale*5, 2.25, 1, easeOut);
ceciSequence.addTween(ceci, "_rotation", 0, 360, 3.25, 1, elasticOut);
ceciSequence.addTween(ceci, "_xscale", ceci._xscale*5, ceci._xscale, 4.25, 1, bounceOut);
ceciSequence.addTween(ceci, "_yscale", ceci._yscale*5, ceci._yscale, 4.25, 1, bounceOut);
ceciSequence.addTween(ceci, "_x", 200, 150, 4.5, .5, bounceOut);

ceciSequence.addTween(bg, "_alpha", bg._alpha, 100, 3, 2, bounceOut);

ceciSequence.start();

loopText = new sweetTween(999, 0);

loopText.addTween(loop, "_x", loop._x, loop._x+250, .5, .75, bounceOut);
loopText.addTween(loop, "_x", loop._x+250, loop._x, 2.5, .75, bounceOut);

loopText.start();


sweetTween.as SOURCE

/*************Continuous Actionsript Tweens - source: www.ursart.com*******************/
import mx.transitions.Tween;
import mx.transitions.easing.*;
import mx.utils.Delegate;

class sweetTween {

      private var cycler:MovieClip;

     
function sweetTween(loops, replayDelay) {
            //trace("class constructor: sweetTween()");
            cycler = _level0.
createEmptyMovieClip("sweetTween" + _level0.getNextHighestDepth(), _level0.getNextHighestDepth());
            cycler.animationLoops = loops;
            cycler.animationDelay = replayDelay;
            cycler.myStartTime = cycler.myCurrentTime = cycler.finishedLoops = 0;

            cycler.movieclips =
new Array();
            cycler.myAttributes =
new Array();
            cycler.startValues =
new Array();
            cycler.endValues =
new Array();
            cycler.startTimes =
new Array();
            cycler.durations =
new Array();
            cycler.easings =
new Array();
            cycler.tweens =
new Array();
            cycler.played =
new Array();
            cycler.executionTimes =
new Array();
            cycler.allTweenStop =
new Array();
            cycler.allTweenResume =
new Array();
            cycler.seriesPaused =
false;
     }
     
public function addTween(mc, attribute, startValue, endValue, startTime, duration, easing) {
        
  //trace("public function: addTween()");
          cycler.movieclips.
push(mc);
          cycler.myAttributes.
push(attribute);
          cycler.startValues.
push(startValue);
          cycler.endValues.
push(endValue);
          cycler.startTimes.
push(startTime);
          cycler.durations.
push(duration);
          cycler.easings.
push(easing);
          cycler.executionTimes.
push(Number(startTime + duration));
          cycler.executionTimes.
sort(sortNumber);
          cycler.timeToWait = cycler.executionTimes[(cycler.executionTimes.
length) - 1] + cycler.animationDelay;
     }
     
public function start() {
         
 //trace("public function: start()");
          cycler.myStartTime =
getTimer();
          cycler.
onEnterFrame = Delegate.create(this, tweenOrLoop);
     }
     
public function remove() {
         
 //trace("public function: remove()");
          for (
var i:Number = 0; i < cycler.movieclips.length; i++) {
               cycler.allTweenStop = cycler.
tweens[i];
               cycler.allTweenStop.
stop();
          }
          removeMovieClip(cycler);
     }
     
private function makeTweens() {
         
 //trace("private function: makeTweens()");
          
for (var i = 0; i < cycler.movieclips.length; i++) {
               
if (cycler.myCurrentTime > cycler.startTimes[i] * 1000 && cycler.played[i] != true) {
                    cycler.
tweens[i] = new mx.transitions.Tween(cycler.movieclips[i], cycler.myAttributes[i], cycler.easings[i], cycler.startValues[i], cycler.endValues[i], cycler.durations[i], true);
                    cycler.played[i] =
true;
               }
          }
     }
     
private function tweenOrLoop() {
         
 //trace("private function: tweenOrLoop");
          cycler.myCurrentTime =
getTimer() - cycler.timeToWait * 1000 * cycler.finishedLoops - cycler.myStartTime;
          
if (cycler.myCurrentTime > cycler.timeToWait * 1000) {
               
if (cycler.finishedLoops < cycler.animationLoops - 1) {
                    nextLoop();
               }
else {
                   for (
var i = 0; i < cycler.movieclips.length; i++) {
                        cycler.played[i] =
false;
                    }
                    cycler.
onEnterFrame = null;
               }
          }
else {
               makeTweens();
          }
     }
     
private function nextLoop() {
         
 //trace("private function: nextLoop()");
          cycler.
onEnterFrame = null;
          
for (var i:Number = 0; i < cycler.movieclips.length; i++) {
               cycler.played[i] =
false;
          }
          cycler.finishedLoops = cycler.finishedLoops + 1;
          cycler.
onEnterFrame = Delegate.create(this, tweenOrLoop);
     }
     
private function sortNumber(a, b) {
          return a - b;
     }
}