当前位置:软件学习 > Flash >>

flash as3 转向行为实现代码

 代码如下 复制代码

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

补充:flash教程,As3.0 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,