使用for循环第2部分进行动画制作

时间:2013-03-24 22:22:23

标签: actionscript-3 flash-builder

我有另一个问题一直困扰着我。这是我的另一个问题"Animating with a for loop?的后续跟进。“好吧,所以我把它弄下来了,但是我怎么能让每个球在舞台上随机出现而不是互相跟随呢?

这是我的代码: 球从名为Ball的外部类中拉出。

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.ui.Keyboard;

    public class Dummy extends Sprite
    {

        private var balls:Array;
        private var ball:Ball;
        private var ballNum: Number = 10;

        private var ax:Number = 4;

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

                balls.push(ball);
            }
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        protected function onEnterFrame(event:Event):void
        {
            for(var i:int = 0; i < balls.length; i++)
            {
                balls[i].x += ax;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

为每个球对象指定一个不同的方向并使用它来移动球而不是ax值。

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.ui.Keyboard;

    public class Dummy extends Sprite
    {

        private var balls:Array;
        private var ball:Ball;
        private var ballNum: Number = 10;

        private var directions:Array = [new Point(-1,-1),new Point(0,-1),new Point(1,-1),
                                new Point(-1,0),new Point(1,0),
                                new Point(-1,1),new Point(0,1),new Point(1,1)];

        public function Dummy()
        {
            init();
        }
        private function init():void
        {
            balls = new Array();

            for(var i:Number = 0; i < ballNum; i++)
            {
                ball = new Ball(Math.random() * 30);
                ball.x = Math.random() * stage.stageWidth;
                ball.y = Math.random() * stage.stageHeight;
                ball.direction = directions[Math.floor(Math.random()*directions.length)];
                addChild(ball);

                balls.push(ball);
            }
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
        }

        protected function onEnterFrame(event:Event):void
        {
            for(var i:int = 0; i < balls.length; i++)
            {
                balls[i].x += balls[i].direction.x;
                balls[i].y += balls[i].direction.y;
            }
        }
    }
}

答案 1 :(得分:0)

为你的Ball类属性提供速度,并提供一个方法move()来调整各自的坐标,并且可能会增强以检查有时候的碰撞,这样你的球就可以反弹了例如。

// Ball class additions follow
private var vx:Number;
private var vy:Number;
public function Ball() {
    // ... original constructor here
    var angle:Number=Math.random(2.0*Math.PI); // random angle
    var vel:Number= MAX_VELOCITY*(0.5+0.5*Math.random()); // random velocity module
    // define this constant^
    vx=vel*Math.cos(angle);
    vy=vel*Math.sin(angle); // composites of velocity
} 
public function move():void {
    this.x+=vx;
    this.y+=vy;
}

// main class
    protected function onEnterFrame(event:Event):void
    {
        for(var i:int = 0; i < balls.length; i++)
        {
            balls[i].move();
        }
    }