动画与一个for循环?动画、for

2023-09-09 21:54:52 作者:短

我使用的是循环多个对象添加到舞台,从那里我想它们的动画,但是当我只尝试了球之一动。

I am using a for loop to add multiple objects to the stage, from there I would like to animate them but when I try only one of the balls move.

下面是我的code。

Here is my code.

(球是从外部类拉动)的

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite
    {

        private var ball:Ball;
        private var ax:Number = 4;

        public function Main()
        {
            init();
        }
        private function init():void
        {
            for(var i:Number = 0; i < 10; i++)
            {
                ball = new Ball();
                ball.x = Math.random() * stage.stageWidth;
                ball.y = Math.random() * stage.stageHeight;
                addChild(ball);
            }

            addEventListener(Event.ENTER_FRAME, onEnterFrame1);
        }
        private function onEnterFrame1(event:Event):void
        {
            ball.x += ax;
        }
    }
}

感谢您!

推荐答案

您应该推动所有对象到数组和更改x属性为每个球。

You should push all your objects into array and change x property for each ball.

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite
    {
        private var ball:Ball;
        private var ax:Number = 4;

        private var balls:Array;

        public function Main()
        {
            init();
        }

        private function init():void
        {
            balls = new Array();
            for(var i:Number = 0; i < 10; i++)
            {
                ball = new Ball();
                ball.x = Math.random() * stage.stageWidth;
                ball.y = Math.random() * stage.stageHeight;
                addChild(ball);

                balls.push(ball);
            }

            addEventListener(Event.ENTER_FRAME, onEnterFrame1);
        }
        private function onEnterFrame1(event:Event):void
        {
            for(var i:int = 0; i < balls.length; i++)
            {
                balls[i].x += ax;
            }
        }
    }
}
 
精彩推荐