动画与一个for循环? [英] Animating with a for loop?

查看:301
本文介绍了动画与一个for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
            }
        }
    }
}

这篇关于动画与一个for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆