flash as3 转向行为实现代码

作者:袖梨 2022-06-28
 代码如下 复制代码

package
{
    import flash.display.Sprite;
    import flash.events.Event;
 
    public class Car2 extends Sprite
    {
        private var vs:Array = [];
        public function Car2()
        {
            var c:int = 10;
            while (c-->0) {
                vs.push(new Vehicle);
            }
            addEventListener(Event.ENTER_FRAME, enterFrame);
        }
 
        private function enterFrame(e:Event):void
        {
            graphics.clear();
            graphics.lineStyle(0);
            for each(var v:Vehicle in vs) {
                v.x += v.vx;
                v.y += v.vy;
                if (v.x < 0) {
                    v.x = 0;
                    v.vx = Math.abs(v.vx);
                }
 
                if (v.x > 400) {
                    v.x = 400;
                    v.vx = -Math.abs(v.vx);
                }
                if (v.y < 0) {
                    v.y = 0;
                    v.vy = Math.abs(v.vy);
                }
 
                if (v.y > 400) {
                    v.y = 400;
                    v.vy = -Math.abs(v.vy);
                }
                graphics.drawCircle(v.x, v.y, 1);
            }
        }
    }
}
 
class Vehicle
{
    public var x:Number = 0;
    public var y:Number = 0;
    public var vx:Number = 2 * Math.random();
    public var vy:Number = 2 * Math.random();
    public var maxSpeed:Number = 5;
    public var mass:Number = 1;
}

相关文章

精彩推荐