A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: assigning tween function in an array through a loop AS3

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    4

    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.

  2. #2
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    Try putting the for loop inside the function.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    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?

  4. #4
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    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 0obj.lengthi++) {
        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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center