Using Actionscript 3 Tween for everything

Blog
Programming

You may probabely know that tween class does not work with anything other than simple numeric object properties. This is an issue sometimes becuase you might want to tween a function! or a property of a child object or even an string! There are plenty of third-party classes out there for extending the Tween class, but if you don't need all other features they offer and prefer to keep your swf file's size small i must tell you that there is a simple workaround.

What we're going to do is using dynamic objects. We create a new object and add a property to it :

var myObject={'percent':0}

Then we tween that property :

var myTween:Tween = new Tween(
	myObject,//Object
	"percent", //Property
...

Now we can access the property within events :

myTween.addEventListener(TweenEvent.MOTION_CHANGE, myTweenOnUpdate);
function myTweenOnUpdate(e:Event):void {
	trace(myObject.percent);
}

 

Sample code :

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

var myObject={'percent':0}
var myTween:Tween = new Tween(
	myObject,//Object
	"percent", //Property
	Strong.easeOut, //Function
	0, //Start Number
	100, //Finish Number
	10, //Duration
	true //useSeconds
);
myTween.addEventListener(TweenEvent.MOTION_CHANGE, myTweenOnUpdate);
function myTweenOnUpdate(e:Event):void {
	trace(myObject.percent);
	
	myFunction(myObject.percent);
	
	if (myObject.percent>10) {
		anotherObject.name='Yes';
	}

	anotherObject.childObject.number=percent;
}
myTween.start()

Resources

Your rating: None Average: 1.3 (86 votes)