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

来源:互联网 发布:spss数据结果分析报告 编辑:程序博客网 时间:2024/05/16 09:20

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package
{
    importflash.display.Bitmap;
    importflash.display.MovieClip;
    importflash.display.Sprite;
    importflash.events.Event;
    importflash.display.StageScaleMode;
    importflash.display.StageAlign;
    importflash.events.MouseEvent;
    importflash.ui.Mouse;
                                                                                
    /**
     * ...
     * @author Childhood
     */
    [SWF(width="454",height="355",frameRate="30")]
    publicclass Main extendsSprite
    {
        [Embed(source = "../res/bg.jpg")]
        privatevar Background:Class;
                                                                                    
        [Embed(source = "../res/Flying.png")]
        privatevar Fly:Class;
        privatevar customMouseCursor:MovieClip;
        privatevar fly:Bitmap;
        privatevar flySprite:Sprite;
        privatevar flySpriteHalfWidth:Number;
        privatevar flySpriteHalfHeight:Number;
        privatevar speed:Number= 10;
        privatevar friction:Number= 0.95;
        privatevar vx:Number= 0;
        privatevar vy:Number= 0;
                                                                                    
        publicfunction Main():void
        {
            if(stage) init();
            elseaddEventListener(Event.ADDED_TO_STAGE, init);
        }
                                                                                    
        privatefunction init(e:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            getStarted();
        }
                                                                                    
        privatefunction getStarted():void
        {
            stage.scaleMode = StageScaleMode.NO_BORDER;
            stage.align = StageAlign.TOP_LEFT;
            Mouse.hide();
            varbg:Bitmap = newBackground();
            addChild(bg);
                                                                                        
            customMouseCursor = newMouseCircle();
            addChild(customMouseCursor);
            fly = newFly();
            //将苍蝇放到一个精灵中,并将注册点移动到精灵的中心
            flySprite = newSprite();
            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);
        }
                                                                                    
        privatefunction flying(e:Event):void
        {
            vardisX:Number= stage.mouseX - flySprite.x;
            vardisY:Number= stage.mouseY - flySprite.y;
            vardis:Number= Math.sqrt(disX * disX + disY * disY);
            varangle: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;
            }
            elseif (flySprite.x - flySpriteHalfWidth < 0) {
                flySprite.x = flySpriteHalfWidth;
                vx *= -1;
            }
            if(flySprite.y - flySpriteHalfHeight < 0) {
                flySprite.y = flySpriteHalfHeight;
                vy *= -1;
            }
            elseif (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;
            }
                                                                            
                                                                                        
        }
                                                                                    
        privatefunction onMouseLeave(e:Event):void
        {
            customMouseCursor.visible = false;
        }
                                                                                    
        privatefunction onMove(e:MouseEvent):void
        {
            customMouseCursor.visible = true;
            customMouseCursor.x = stage.mouseX;
            customMouseCursor.y = stage.mouseY;
        }
                                                                                    
    }
                                                                                
}

 

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


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

原创粉丝点击