-
assigning tween function to an array through a loop AS3
Hi, I have been trying to assign tween function in an array through a loop in as3.
I have 20 mc on stage, the instance names are obj1, obj2 and so on.
I want to assign a tween function to each of them, the function looks like this :
Actionscript Code:
function startTween(e:TweenEvent = null):void { xTween = new Tween(obj1, "x", None.easeNone, obj1.x, Math.random()*20, 0.2, true);
xTween.addEventListener(TweenEvent.MOTION_FINISH,startTween); }
it worked, but only for obj1.
so I thought maybe I can put them in an array, var obj:Array = new Array(obj1, obj2.....);
and then I'll run a loop through it.
Actionscript Code:
for(var i:uint = 0; i < obj.length; i++) { function startTween(e:TweenEvent = null):void { xTween = new Tween(obj[i], "x", None.easeNone, obj[i].x, Math.random()*20, 0.2, true); xTween.addEventListener(TweenEvent.MOTION_FINISH,startTween); } }
it doesn't work.I am not sure what went wrong.
it gave me this Error message: #1010: A term is undefined and has no properties.
Can anybody help? thanks
Last edited by mudman; 03-17-2010 at 09:27 AM.
-
Try putting the for loop inside the function.
-
you mean like this?
Actionscript Code:
function startTween(e:TweenEvent = null):void { for(var i:uint = 0; i < obj.length; i++) { xTween = new Tween(obj[i], "x", None.easeNone, obj[i].x, Math.random()*20, 0.2, true); xTween.addEventListener(TweenEvent.MOTION_FINISH,startTween); } }
isn't that an infinite loop?
-
You forgot to import the packages and the function is in the wrong place.
PHP Code:
//Import the packages needed for tweening.
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;
//Array to hold all the object instances.
var obj:Array=new Array(obj1,obj2,obj3);
//Loops through all the movie clips in the array.
for (var i:uint = 0; i < obj.length; i++) {
var xTween=new Tween(obj[i],"x",None.easeNone,obj[i].x,Math.random()*20,0.2,true);
}
//Listens to when the tween has finished
xTween.addEventListener(TweenEvent.MOTION_FINISH,startTween);
//Displays message when tween is finished.
function startTween(event:TweenEvent):void {
trace("finished tweening");
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|