AI中的漫游行为,简单模拟了下

package
{
    import flash.display.Bitmap;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
                                                                                
    /**
     * ...
     * @author  Childhood
     */
    [SWF(width="454",height="355",frameRate="30")]
    public class Main extends Sprite 
    {
        [Embed(source = "../res/bg.jpg")]
        private var Background:Class;
                                                                                    
        [Embed(source = "../res/Flying.png")]
        private var Fly:Class;
        private var customMouseCursor:MovieClip;
        private var fly:Bitmap;
        private var flySprite:Sprite;
        private var flySpriteHalfWidth:Number;
        private var flySpriteHalfHeight:Number;
        private var speed:Number = 10;
        private var friction:Number = 0.95;
        private var vx:Number = 0;
        private var vy:Number = 0;
                                                                                    
        public function Main():void
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
                                                                                    
        private function init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            getStarted();
        }
                                                                                    
        private function getStarted():void
        {
            stage.scaleMode = StageScaleMode.NO_BORDER;
            stage.align = StageAlign.TOP_LEFT;
            Mouse.hide();
            var bg:Bitmap = new Background();
            addChild(bg);
                                                                                        
            customMouseCursor = new MouseCircle();
            addChild(customMouseCursor);
            fly = new Fly();
            //将苍蝇放到一个精灵中,并将注册点移动到精灵的中心
            flySprite = new Sprite();
            fly.x = -fly.width / 2;
            fly.y = -fly.height / 2;
            flySprite.addChild(fly);
            flySpriteHalfWidth = flySprite.width / 2;
            flySpriteHalfHeight = flySprite.height / 2;
            addChild(flySprite);
            fly.addEventListener(Event.ENTER_FRAME, flying);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
            stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
        }
                                                                                    
        private function flying(e:Event):void
        {
            var disX:Number = stage.mouseX - flySprite.x;
            var disY:Number = stage.mouseY - flySprite.y;
            var dis:Number = Math.sqrt(disX * disX + disY * disY);
            var angle:Number = Math.atan2(disY, disX);
                                                                                        
            //shake的关键性代码
            vx += (Math.random() * 0.2 - 0.1) * 15; 
            vy += (Math.random() * 0.2 - 0.1) * 15; 
                                                                                        
            vx *= friction;
            vy *= friction;
                                                                                        
            flySprite.x += vx;
            flySprite.y += vy;
            //边缘检测
            if (flySprite.x + flySpriteHalfWidth > stage.stageWidth) {
                flySprite.x = stage.stageWidth - flySpriteHalfWidth;
                vx *= -1;
            }
            else if (flySprite.x - flySpriteHalfWidth < 0) {
                flySprite.x = flySpriteHalfWidth;
                vx *= -1;
            }
            if (flySprite.y - flySpriteHalfHeight < 0) {
                flySprite.y = flySpriteHalfHeight;
                vy *= -1;
            }
            else if (flySprite.y + flySpriteHalfHeight > stage.stageHeight) {
                flySprite.y = stage.stageHeight - flySpriteHalfHeight;
                vy *= -1;
            }
            //靠近鼠标的反应
            if (Math.abs(flySprite.x - stage.mouseX) < 50) {
                if (Math.abs(flySprite.y - stage.mouseY) < 50) {
                    //flySprite.x += -vx;
                    //flySprite.y += -vy;
                    flySprite.x += (Math.random() * 0.2 - 0.1) * 30 + 3; 
                    flySprite.y += (Math.random() * 0.2 - 0.1) * 30 + 3; 
                    flySprite.rotation = Math.random() * 360;
                    flySprite.scaleX = 0.8;
                    flySprite.scaleY = 0.8;
                }
            }
            else {
                flySprite.scaleX = flySprite.scaleY = 1;
            }
                                                                            
                                                                                        
        }
                                                                                    
        private function onMouseLeave(e:Event):void
        {
            customMouseCursor.visible = false;
        }
                                                                                    
        private function onMove(e:MouseEvent):void
        {
            customMouseCursor.visible = true;
            customMouseCursor.x = stage.mouseX;
            customMouseCursor.y = stage.mouseY;
        }
                                                                                    
    }
                                                                                
}

做了一个很简单的效果,就是一个模拟苍蝇飞行的效果。当苍蝇碰到墙壁后会弹回来,碰到鼠标后会有有趣的效果。

看效果演示

源码文件下载:http://115.com/file/e7xa2box#AI漫游行为.rar

你可能感兴趣的:(游戏AI,漫游)