[flash player]flash as3 转向行为实现代码

更新时间:2017-05-31    来源:php常用代码    手机版     字体:

【www.bbyears.com--php常用代码】

 代码如下

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

本文来源:http://www.bbyears.com/jiaocheng/33036.html

猜你感兴趣

热门标签

更多>>

本类排行