0

我还有一个问题一直困扰着我。这有点像我的另一个问题“使用 for 循环动画? ”的后续行动

这是我的代码: ball 是从名为 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;
            }
        }
    }
}
4

2 回答 2

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;
            }
        }
    }
}
于 2013-03-24T22:35:37.197 回答
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();
        }
    }
于 2013-03-25T04:23:08.290 回答