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

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
{
     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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值