1 // 游戏的主逻辑循环
  2 function GameRun()
  3 {
  4     // 如果敌人全数被歼灭,游戏结束
  5     if (current_enemy_sn > enemy_total)
  6     {
  7         game_end();
  8     } 
  9     
 10     // 如果敌人已死,创建新敌人,或者游戏结束
 11     if (current_enemy == null)
 12     {
 13         //  敌人死亡后,需要显示一段时间以表达效果
 14         if (time_current_died > time_enemy_out)
 15         {
 16             create_enemy();   // 创建敌人
 17             enemy_run();   // 敌人开始跑动
 18         }
 19         else
 20         {
 21             time_current_died = time_current_died + game_speed;  // 延时
 22         } 
 23     } 
 24     if (current_enemy == null)
 25     {
 26         return;
 27     } 
 28     if (current_enemy._x > world_width)
 29     {
 30         enemy_run();
 31         ++missed;
 32     } 
 33     current_enemy._x = current_enemy._x + current_enemy_speed;
 34     if (!weapon_fired)
 35     {
 36         return;
 37     } 
 38     if (current_weapon._y < -current_weapon._height)
 39     {
 40         current_weapon._y = world_height - current_weapon._height;
 41         weapon_fired = false;
 42     } 
 43     current_weapon._y = current_weapon._y - current_weapon_speed;
 44     if (hitEnemy())
 45     {
 46         enemyDead();
 47     } 
 48 
 49 
 50 // 敌人死亡
 51 function enemyDead()
 52 {
 53     current_enemy.gotoAndStop("dead");
 54     current_enemy._alpha = 80;
 55     current_weapon._x = current_enemy._x + current_enemy._width / 2;
 56     current_weapon._y = current_enemy._y;
 57     current_enemy = null;
 58     time_current_died = 0;
 59     weapon_fired = false;
 60     ++caughted;
 61     calc_score();
 62     playSound("died_cry");
 63 
 64 
 65 // 发出武器
 66 function fireWeapon()
 67 {
 68     if (!weapon_fired)
 69     {
 70         playSound("knife");
 71         weapon_fired = true;
 72     } 
 73 
 74 
 75 // 创建新的敌人
 76 function create_enemy()
 77 {
 78     current_enemy = s1;
 79     current_enemy.gotoAndPlay(1);
 80     current_enemy._alpha = 100;
 81     current_weapon._y = world_height - current_weapon._height;
 82 
 83 
 84 // 敌人跑过
 85 function enemy_run()
 86 {
 87     current_enemy._x = -current_enemy._width;
 88     ++current_enemy_sn;
 89 }
 90 
 91 // 击中敌人
 92 function hitEnemy()
 93 {
 94     return (current_weapon._y < current_enemy._y + current_enemy._height &&
 95 current_weapon._y > current_enemy._y - current_weapon._height && 
 96 current_weapon._x > current_enemy._x && 
 97 current_weapon._x < current_enemy._x + current_enemy._width - current_weapon._width);
 98 
 99 
100 // 计算成绩
101 function calc_score()
102 {
103     score = caughted * 10;
104 
105 
106 // 播放声音
107 function playSound(sndName)
108 {
109     var _loc1 = new Sound();
110     _loc1.attachSound(sndName);
111     _loc1.start();
112 
113 
114 // 游戏结束
115 function game_end()
116 {
117     current_enemy = null;
118     current_weapon = null;
119     clearInterval(world_timer);
120     gotoAndPlay("Game_End");
121 
122 
123 var score = 0;        // 得分
124 var caughted = 0;   // 击中的数量
125 var missed = 0;  // 脱逃的数量
126 var enemy_total = 10;  // 每次游戏中敌人的数量
127 var current_enemy_sn = 1;   // 当前敌人的序号
128 var current_enemy = s1;   // 当前的敌人组件名称
129 var current_weapon = k;   // 当前武器的名称
130 var weapon_fired = false;   // 武器是否激发
131 var time_enemy_out = 1000;   // 延时时长
132 var time_current_died = 0;   // 死亡后的时间
133 var current_enemy_speed = 18;   // 敌人移动的速度
134 var current_weapon_speed = 36;  // 武器攻击的速度
135 var game_speed = 50;   // 游戏主逻辑循环速度
136 var world_height = 400;  // 游戏世界的高度
137 var world_width = 550;  // 游戏世界的宽度
138 
139 // 两种游戏背景下,根据画面现实需要,将角色放置到合适的出发位置
140 if (Scene1._visible)
141 {
142     s1._y = 180;
143 
144 if (Scene2._visible)
145 {
146     s1._y = 93;
147 
148 
149 // 游戏世界的时钟
150 var world_timer = setInterval(GameRun, game_speed);
151 
152 // 重启游戏
153 BtnReset.onPress = function ()
154 {
155     clearInterval(world_timer);
156     gotoAndPlay("Game_Face");
157 };
158 
159 // 武器发射
160 k.onPress = function ()
161 {
162     fireWeapon();
163 };
164 stop ();
posted on 2011-05-27 18:37  精思入神  阅读(1242)  评论(0编辑  收藏  举报