Search code examples
actionscript-3flashactionscriptflash-cs6

Why the Tween Class don't work inside a for with Flash AS3?


Problem


I'm trying to make a Tween with many Movieclips using the for command, like this:

for(var i:int = 0; i < mcArray.length; i++) {

    new Tween(mcArray[i], "x", Regular.easeOut, 0, 100, 1.0, true);

}

But it doesn't works. I've tried change the code as follow down:

var tween:Tween = new Tween(mcArray[i], "x", Regular.easeOut, 0, 100, 1.0, true);

And it doesn't works too.

I can't use the setInterval or Timer because the Movieclips should be synchronized and the it may cause problems.

Is there a way to do this?


Solution

  • AS3 Tween engine, very bad: by design, by performance etc. If you can't use Greensock library for animation, you could use GTween, It's great tweening engine under MIT Licence. Even without support and continuous improvement, engine is much better than Tween by Adobe.

    Working example, after start, if will collect every object in the center of scene:

    package {
    
        import com.gskinner.motion.GTween;
        import com.gskinner.motion.easing.Sine;
    
        import flash.display.DisplayObject;
        import flash.display.Shape;
        import flash.display.Sprite;
        import flash.display.StageAlign;
        import flash.display.StageScaleMode;
        import flash.events.Event;
        import flash.geom.Point;
    
        public class StackOverflow extends Sprite {
    
            public function StackOverflow() {
                addEventListener(Event.ADDED_TO_STAGE, onAdded);
            }
    
            private function onAdded(e:Event):void {
                removeEventListener(Event.ADDED_TO_STAGE, onAdded);
    
                stage.align = StageAlign.TOP_LEFT;
                stage.scaleMode = StageScaleMode.NO_SCALE;
    
                setup();
            }
    
            private function createDummyObjects(size:uint):Array {
                var result:Array = [], i:uint, object:Shape;
                for (i; i < size; ++i) {
                    object = new Shape();
                    object.graphics.beginFill(Math.random() * 0xFFFFFF);
                    object.graphics.drawCircle(0, 0, 20);
                    result[i] = object;
                }
                return result;
            }
    
            private function setup():void {
                var objects:Array = createDummyObjects(10);
                var i:uint, len:uint = objects.length, item:DisplayObject, middle:Point = new Point(stage.stageWidth >> 1, stage.stageHeight >> 1);
    
                for (i; i < len; ++i) {
                    item = objects[i];
                    item.x = Math.random() * stage.stageWidth;
                    item.y = Math.random() * stage.stageHeight;
    
                    addChild(item);
    
                    new GTween(item, 1 + Math.random() * 4, {x: middle.x, y: middle.y}, {ease: Sine.easeInOut});
                }
            }
    
        }
    }