Default Macromedia Tween class has many limits. One of that limits is that it can’t tween objects or its properties. Let say you need to tween movie RGB colors which are tightly connected with ColorTransform and Transform objects. During achivement of this goal I try to reuse Macromedia Tween class original code more as I can. Less code for me less changes for you. This tutorial also cover inheritance(extending, subclass or it was superclass who can remember all that different languages different keywords on same ball) and customization to make your developer live easier.
First, i have to figure out how Tween class working by digging in Tween.as torned from Flash8 “sophisticated” debugger (TweenMa.as in download files) (Let me found MovieClip implementation this way ?!
) )
Tween class has a constructor like this:
obj is object(most of the cases MovieClip) on its propertie prop is applied external(incrementing/decrementing) function func (like some Easing.Elastic) or internal(incrementing/decrementing) linear function if func is null from starting propertie value begin to ending propertie value finish. Eather you use duration in seconds(useSeconds=true) or number of frames (useSeconds=false) is dependable only on FPS (frames per second) and not with real time. (Uhhhh i’made that in 6 rows)
At the begining there was Tween and I extend it:
![]()
To achive our goal and tween object (its properties) in our case ColorTransform object:
we must to extend begin, finish, change…. variables to type Objects from formerly Number type
Also we must to change all operation between them becouse change:Number=begin:Number-finish:Number its not the same as change:Object=begin:Object-finish:Object. I try to find someting about overloading operators but i’ve found that is not possible in AS2 so i’ve made simple class OperatorsEx. Let see exmple how String and Number addition can be to transformed to include also Object addition :
function OperatorsEx()
{
}
function add(a:Object,b:Object,d:Object)//d:Object for custom properties
{
var prop,c:Object;
if(a==undefined || b==undefined)
{
throw(”operands undefined”);
return null;
}
if(typeof(b)!=typeof(a))
{
throw(”operands are not the same type”);
return null;
}
if(typeof(a)==”object”)
{
c=new Object();
if(d!=undefined)
for(prop in d)
c[prop]=a[prop]+b[prop];
else
for(prop in a)
c[prop]=a[prop]+b[prop];
}
else
{
c=a+b;
}
return c ;
}
…….
So new overloaded function are looking like this:
function get finish ():Object {
return op.add(this.begin,this.change,propObject);
}
where op is op=new OperatorEx.
TweenEx constructor have all input parametars as Tween consturctor + extended:Boolean (which define if u wanna use Ex(tended) or classic features) + exParam1,exParam2 which are input parametars.
this.currentMovie=MovieClip(obj);
if(!extended || extended==undefined)
stdTween(obj, prop, func, begin, finish, duration, useSeconds);
//super.Tween(obj, prop, func, begin, finish, duration, useSeconds); //should but won’t???
else
exTween(obj, prop, func, begin, finish, duration, useSeconds,extended,exParam1,exParam2);//
}
If u choose extended tween prop(rgb,bezier,redOffset…) by setting extended constructor parameter to true you will initiate different constructor. Becouse this class was first designated as extension for “rgb” tween i will describe colorRGBTween constructor.
It takes begin-finish hex or RGBA values.Then initiate and set tweening object (in our case Transform object applied on our MovieClip obj) and its “colorTransform” prop which will be tweened:
Then set our custom tweening function (function that caluculate incrementing/decrementig peaces every interval t) rgbTween func:
which look like this:
clrTrans[prop]=c[prop]*t/d+b[prop];
}
return clrTrans;
}
Note: In code you can find function “std” that is a default Tween class tweening function and compare with “rgbTween” or “bezierTween” as a example writing your own.
Function setPosition has task to apply tweening function calculated peace to tweened object properties.
Not satisfy with tween starting after constructor inicialization, i’ve commented line ” this.start()” in the constructor, so you must start tweening where you want and when you want like this:
I’ve overload yoyo function to achive “yoyo to random value(color)”:
case “redOffset”: //not tested
case “greenOffset”:
case “blueOffset”:
this.begin=this.finish;
this.finish=randRange(0,255);
break;
case “ColorTransform”:
alpha=begin.alphaOffset;
this.begin=this.finish;
//create new random color
this.finish=rgbObject(randRange(0,255)+”,”
+randRange(0,255)+”,”+randRange(0,255)+”,
“+Math.round((alpha+255)/2.55));
break;
}
this.start();
}
}
else
this.continueTo (this.begin, this.time);
};
Note1: Some code is //not tested cos of changes during writing this tutorial.
Note2: How to use other TweenEx class tweening pls review TweenEx.as
Support functions:
function rgbObject(strColor:String):ColorTransform – allows hex or (RGBA) entry.
Support functions:
function isNaRGB(numColor):Boolean – check if number is RGB.
Support functions:
function randRange(min:Number, max:Number):Number – Macromedia random ex. function.
| » Download |
by Winx in flashkit
July 2nd, 2007 at 9:42 am
the same thing ca be achived using the Extend Tweening Engine from Extend Studio ( http://www.extendstudio.com/Products/Extend_Tweening_Engine/Overview/ )
October 31st, 2007 at 11:55 pm
hi there,
in the as2lib framework you can find a way to overload in flash…
great stuff, keep it comming…