一聚教程网:一个值得你收藏的教程网站

热门教程

flash as3 转向行为实现代码

时间:2022-06-28 22:16:14 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

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

热门栏目