package
{
import flash.display. MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.text.*;
public class Main extends Sprite
{
public static const STATE_INIT:int = 10;
public static const STATE_START_PLAYER:int = 20;
public static const STATE_PLAY_GAME:int = 30;
public static const STATE_REMOVE_PLAYER:int = 40;
public static const STATE_END_GAME:int = 50;
public var gameState:int = 0; // 游戲狀態
public var score:int = 0; // 玩家的分數
public var chances:int = 0; // 漏掉的氣球數
public var bg: MovieClip; // 游戲背景
public var enemies:Array; // 存儲敵人的數組
public var missiles:Array; // 玩家發射的導彈
public var explosions:Array; // 爆炸特效
public var player: MovieClip; // 引用玩家的圖片
public var level: Number; // 等級
public var scoreLabel:TextField = new TextField(); // 分數標題
public var levelLabel:TextField = new TextField(); // 等級標題
public var chancesLabel:TextField = new TextField(); // 漏網標題
public var scoreText:TextField = new TextField(); // 分數值
public var levelText:TextField = new TextField(); // 等級值
public var chancesText:TextField = new TextField(); // 漏網值
public const SCOREBOARD_Y: Number = 5; // 計分板位於游戲頂部
public function Main():void
{
init();
}
private function init():void
{
this.addEventListener(Event.ENTER_FRAME, gameLoop);
// 顯示背景
bg = new BackImage();
this.addChild(bg);
// 設置計分板標題和默認值
scoreLabel.text = "得分:";
levelLabel.text = "等級:";
chancesLabel.text = "生命:";
scoreText.text = "0";
levelText.text = "1";
chancesText.text = "3";
scoreLabel.textColor = 0xffffff;
levelLabel.textColor = 0xffffff;
chancesLabel.textColor = 0xffffff;
scoreText.textColor = 0xffffff;
levelText.textColor = 0xffffff;
chancesText.textColor = 0xffffff;
// 放置游戲底部
scoreLabel.y = SCOREBOARD_Y;
levelLabel.y = SCOREBOARD_Y;
chancesLabel.y = SCOREBOARD_Y;
scoreText.y = SCOREBOARD_Y;
levelText.y = SCOREBOARD_Y;
chancesText.y = SCOREBOARD_Y;
// 設置標題和值橫向坐標
scoreLabel.x = 5;
scoreText.x = 50;
chancesLabel.x = 105;
chancesText.x = 155;
levelLabel.x = 205;
levelText.x = 260;
// 顯示計分板
this.addChild(scoreLabel);
this.addChild(levelLabel);
this.addChild(chancesLabel);
this.addChild(scoreText);
this.addChild(levelText);
this.addChild(chancesText);
gameState = STATE_INIT;
}
// 調整游戲狀態的循環函數
public function gameLoop(e:Event):void
{
switch(gameState)
{
case STATE_INIT:
initGame();
break;
case STATE_START_PLAYER:
startPlayer();
break;
case STATE_PLAY_GAME:
playGame();
break;
case STATE_REMOVE_PLAYER:
removePlayer();
break;
case STATE_END_GAME:
endGame();
break;
}
}
// 初始化游戲
public function initGame():void
{
// 當點擊鼠標時,創建玩家發射的導彈
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
// 初始化游戲
score = 0; // 分數
chances = 3; // 飛船數
enemies = new Array(); // 敵人數組
missiles = new Array();
explosions = new Array();
level = 1;
levelText.text = level.toString();
player = new PlayerImage();
gameState = STATE_START_PLAYER;
}
public function startPlayer():void
{
this.addChild(player);
player.startDrag( true, new Rectangle(0,320,550,365));
gameState = STATE_PLAY_GAME;
}
public function removePlayer():void
{
// 從敵人數組反向遍歷出一個個敵人
for( var i:int = enemies.length-1; i>=0; i--)
{
removeEnemy(i);
}
for(i = missiles.length-1; i>= 0; i--)
{
removeMissile(i);
}
this.removeChild(player);
gameState = STATE_START_PLAYER;
}
// 開始游戲
public function playGame():void
{
// 創建敵人函數
makeEnemies();
// 更新祈求Y坐標以及飄出屏幕時移除掉
moveEnemies();
// 檢測玩家是否與敵人發生了碰撞,如果是的話就觸發一事件
testCollisions();
// 查看等級(level)是否可增加或游戲是否可結束
testForEnd();
}
public function makeEnemies():void
{
// 獲取一個0-99之間的隨機數
var chance: Number = Math.floor(Math.random() * 100);
var tempEnemy: MovieClip;
// 等級越大,氣球出現的概率越高
if(chance < level+2)
{
// 創建敵人
var feiji: Number = Math.floor(Math.random() * 45);
if(feiji < 17)
{
tempEnemy = new EnemyImage();
}
else if(feiji >= 17 && feiji < 34)
{
tempEnemy = new EnemyImage1();
}
else if(feiji >= 34 && feiji < 45)
{
tempEnemy = new EnemyImageBoss();
}
// 等級越高,氣球速度越快
tempEnemy.speed = level + 1;
// 敵人出現的y坐標
tempEnemy.y = -25;
// 氣球X軸出現的坐標,由於屏幕是0-540,所以敵人(本身有30個寬度)從0-514中隨機刷出
tempEnemy.x = Math.floor(Math.random() * 515);
// 敵人顯示到場景
this.addChild(tempEnemy);
// 添加到敵人數組中,以便跟蹤
enemies.push(tempEnemy);
}
}
// 遍歷敵人數組,更新每個敵人Y坐標
public function moveEnemies():void
{
var tempEnemy: MovieClip;
for( var i:int = enemies.length - 1; i >= 0; i--)
{
tempEnemy = enemies[i];
tempEnemy.y += tempEnemy.speed;
var pp: Number = Math.floor(Math.random() * 4);
if(pp < 3)
{
tempEnemy.x -= pp;
}
else
{
tempEnemy.x += pp;
}
// 若敵人飛出屏幕底部了
if(tempEnemy.y > 435)
{
removeEnemy(i);
}
}
// 移除子彈
var tempMissile: MovieClip;
for(i = missiles.length-1; i>=0; i--)
{
tempMissile = missiles[i];
tempMissile.y -= tempMissile.speed;
if(tempMissile.y < -35)
{
removeMissile(i);
}
}
// 移除爆炸效果
var tempExplosion: MovieClip;
for(i = explosions.length-1; i>= 0;i--)
{
tempExplosion = explosions[i];
if(tempExplosion.currentFrame >= tempExplosion.totalFrames)
{
removeExplosion(i);
}
}
}
// 子彈和敵人碰撞檢測、自己和敵人碰撞檢測
public function testCollisions():void
{
var tempEnemy: MovieClip; // 當前敵人
var tempMissile: MovieClip; // 當前發射的導彈
// 遍歷敵人數組
for( var i:int = enemies.length - 1; i >= 0; i--)
{
// 循環到的當前敵人對象
tempEnemy = enemies[i];
// 遍歷發射的導彈數組
for( var j:int = missiles.length-1; j>=0; j--)
{
// 循環到的當前導彈對象
tempMissile = missiles[j];
// 如果當前敵人和當前導彈發生碰撞
if(tempEnemy.hitTestObject(tempMissile))
{
score++; // 積分+1
scoreText.text = score.toString();
// 調用爆炸特效(傳遞敵人的x,y坐標,在該坐標上爆炸)
makeExplosion(tempEnemy.x + (tempEnemy.width / 2), tempEnemy.y + (tempEnemy.height / 2));
removeEnemy(i); // 移除當前敵人
removeMissile(j); // 移除當前導彈
break;
}
}
}
// 檢測自己和敵人是否有碰撞
for(i = enemies.length-1; i>=0;i--)
{
// 循環到的當前敵人
tempEnemy = enemies[i];
// 如果當前敵人和自己發生碰撞
if(tempEnemy.hitTestObject(player))
{
chances--; // 生命-1
chancesText.text = chances.toString();
makeExplosion(player.x + (player.width / 2), player.y + (player.height / 2));
gameState = STATE_REMOVE_PLAYER;
}
}
}
// 創建並播放爆炸動畫
public function makeExplosion(ex: Number, ey: Number)
{
// 創建爆炸對象
var tempExplosion: MovieClip = new ExplosionImage();
tempExplosion.x = ex;
tempExplosion.y = ey;
this.addChild(tempExplosion);
// 由於爆炸動畫有7幀,會自動播放,所以放到爆炸數組里以后控制它們
explosions.push(tempExplosion);
// 播放爆炸聲音
var sound:Sound = new Explode();
sound.play();
}
// 查看等級(level)是否可增加或游戲是否可結束
public function testForEnd():void
{
if(chances <= 0)
{
removePlayer();
gameState = STATE_END_GAME;
}
else if(score > level * 30)
{
level++;
levelText.text = level.toString();
}
}
public function removeEnemy(idx:int)
{
this.removeChild(enemies[idx]);
enemies.splice(idx,1);
}
public function removeMissile(idx:int)
{
this.removeChild(missiles[idx]);
missiles.splice(idx,1);
}
public function removeExplosion(idx:int)
{
this.removeChild(explosions[idx]);
explosions.splice(idx,1);
}
public function onMouseDownEvent(e:MouseEvent)
{
if(gameState == STATE_PLAY_GAME)
{
// 創建子彈對象
var tempMissile: MovieClip = new MissileImage();
tempMissile.x = player.x + (player.width / 2);
tempMissile.y = player.y;
tempMissile.speed = 20;
missiles.push(tempMissile);
this.addChild(tempMissile);
var sound:Sound = new Shoot();
sound.play();
}
}
// 結束游戲
public function endGame():void
{
trace("游戲結束!");
}
}
}
{
import flash.display. MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.text.*;
public class Main extends Sprite
{
public static const STATE_INIT:int = 10;
public static const STATE_START_PLAYER:int = 20;
public static const STATE_PLAY_GAME:int = 30;
public static const STATE_REMOVE_PLAYER:int = 40;
public static const STATE_END_GAME:int = 50;
public var gameState:int = 0; // 游戲狀態
public var score:int = 0; // 玩家的分數
public var chances:int = 0; // 漏掉的氣球數
public var bg: MovieClip; // 游戲背景
public var enemies:Array; // 存儲敵人的數組
public var missiles:Array; // 玩家發射的導彈
public var explosions:Array; // 爆炸特效
public var player: MovieClip; // 引用玩家的圖片
public var level: Number; // 等級
public var scoreLabel:TextField = new TextField(); // 分數標題
public var levelLabel:TextField = new TextField(); // 等級標題
public var chancesLabel:TextField = new TextField(); // 漏網標題
public var scoreText:TextField = new TextField(); // 分數值
public var levelText:TextField = new TextField(); // 等級值
public var chancesText:TextField = new TextField(); // 漏網值
public const SCOREBOARD_Y: Number = 5; // 計分板位於游戲頂部
public function Main():void
{
init();
}
private function init():void
{
this.addEventListener(Event.ENTER_FRAME, gameLoop);
// 顯示背景
bg = new BackImage();
this.addChild(bg);
// 設置計分板標題和默認值
scoreLabel.text = "得分:";
levelLabel.text = "等級:";
chancesLabel.text = "生命:";
scoreText.text = "0";
levelText.text = "1";
chancesText.text = "3";
scoreLabel.textColor = 0xffffff;
levelLabel.textColor = 0xffffff;
chancesLabel.textColor = 0xffffff;
scoreText.textColor = 0xffffff;
levelText.textColor = 0xffffff;
chancesText.textColor = 0xffffff;
// 放置游戲底部
scoreLabel.y = SCOREBOARD_Y;
levelLabel.y = SCOREBOARD_Y;
chancesLabel.y = SCOREBOARD_Y;
scoreText.y = SCOREBOARD_Y;
levelText.y = SCOREBOARD_Y;
chancesText.y = SCOREBOARD_Y;
// 設置標題和值橫向坐標
scoreLabel.x = 5;
scoreText.x = 50;
chancesLabel.x = 105;
chancesText.x = 155;
levelLabel.x = 205;
levelText.x = 260;
// 顯示計分板
this.addChild(scoreLabel);
this.addChild(levelLabel);
this.addChild(chancesLabel);
this.addChild(scoreText);
this.addChild(levelText);
this.addChild(chancesText);
gameState = STATE_INIT;
}
// 調整游戲狀態的循環函數
public function gameLoop(e:Event):void
{
switch(gameState)
{
case STATE_INIT:
initGame();
break;
case STATE_START_PLAYER:
startPlayer();
break;
case STATE_PLAY_GAME:
playGame();
break;
case STATE_REMOVE_PLAYER:
removePlayer();
break;
case STATE_END_GAME:
endGame();
break;
}
}
// 初始化游戲
public function initGame():void
{
// 當點擊鼠標時,創建玩家發射的導彈
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownEvent);
// 初始化游戲
score = 0; // 分數
chances = 3; // 飛船數
enemies = new Array(); // 敵人數組
missiles = new Array();
explosions = new Array();
level = 1;
levelText.text = level.toString();
player = new PlayerImage();
gameState = STATE_START_PLAYER;
}
public function startPlayer():void
{
this.addChild(player);
player.startDrag( true, new Rectangle(0,320,550,365));
gameState = STATE_PLAY_GAME;
}
public function removePlayer():void
{
// 從敵人數組反向遍歷出一個個敵人
for( var i:int = enemies.length-1; i>=0; i--)
{
removeEnemy(i);
}
for(i = missiles.length-1; i>= 0; i--)
{
removeMissile(i);
}
this.removeChild(player);
gameState = STATE_START_PLAYER;
}
// 開始游戲
public function playGame():void
{
// 創建敵人函數
makeEnemies();
// 更新祈求Y坐標以及飄出屏幕時移除掉
moveEnemies();
// 檢測玩家是否與敵人發生了碰撞,如果是的話就觸發一事件
testCollisions();
// 查看等級(level)是否可增加或游戲是否可結束
testForEnd();
}
public function makeEnemies():void
{
// 獲取一個0-99之間的隨機數
var chance: Number = Math.floor(Math.random() * 100);
var tempEnemy: MovieClip;
// 等級越大,氣球出現的概率越高
if(chance < level+2)
{
// 創建敵人
var feiji: Number = Math.floor(Math.random() * 45);
if(feiji < 17)
{
tempEnemy = new EnemyImage();
}
else if(feiji >= 17 && feiji < 34)
{
tempEnemy = new EnemyImage1();
}
else if(feiji >= 34 && feiji < 45)
{
tempEnemy = new EnemyImageBoss();
}
// 等級越高,氣球速度越快
tempEnemy.speed = level + 1;
// 敵人出現的y坐標
tempEnemy.y = -25;
// 氣球X軸出現的坐標,由於屏幕是0-540,所以敵人(本身有30個寬度)從0-514中隨機刷出
tempEnemy.x = Math.floor(Math.random() * 515);
// 敵人顯示到場景
this.addChild(tempEnemy);
// 添加到敵人數組中,以便跟蹤
enemies.push(tempEnemy);
}
}
// 遍歷敵人數組,更新每個敵人Y坐標
public function moveEnemies():void
{
var tempEnemy: MovieClip;
for( var i:int = enemies.length - 1; i >= 0; i--)
{
tempEnemy = enemies[i];
tempEnemy.y += tempEnemy.speed;
var pp: Number = Math.floor(Math.random() * 4);
if(pp < 3)
{
tempEnemy.x -= pp;
}
else
{
tempEnemy.x += pp;
}
// 若敵人飛出屏幕底部了
if(tempEnemy.y > 435)
{
removeEnemy(i);
}
}
// 移除子彈
var tempMissile: MovieClip;
for(i = missiles.length-1; i>=0; i--)
{
tempMissile = missiles[i];
tempMissile.y -= tempMissile.speed;
if(tempMissile.y < -35)
{
removeMissile(i);
}
}
// 移除爆炸效果
var tempExplosion: MovieClip;
for(i = explosions.length-1; i>= 0;i--)
{
tempExplosion = explosions[i];
if(tempExplosion.currentFrame >= tempExplosion.totalFrames)
{
removeExplosion(i);
}
}
}
// 子彈和敵人碰撞檢測、自己和敵人碰撞檢測
public function testCollisions():void
{
var tempEnemy: MovieClip; // 當前敵人
var tempMissile: MovieClip; // 當前發射的導彈
// 遍歷敵人數組
for( var i:int = enemies.length - 1; i >= 0; i--)
{
// 循環到的當前敵人對象
tempEnemy = enemies[i];
// 遍歷發射的導彈數組
for( var j:int = missiles.length-1; j>=0; j--)
{
// 循環到的當前導彈對象
tempMissile = missiles[j];
// 如果當前敵人和當前導彈發生碰撞
if(tempEnemy.hitTestObject(tempMissile))
{
score++; // 積分+1
scoreText.text = score.toString();
// 調用爆炸特效(傳遞敵人的x,y坐標,在該坐標上爆炸)
makeExplosion(tempEnemy.x + (tempEnemy.width / 2), tempEnemy.y + (tempEnemy.height / 2));
removeEnemy(i); // 移除當前敵人
removeMissile(j); // 移除當前導彈
break;
}
}
}
// 檢測自己和敵人是否有碰撞
for(i = enemies.length-1; i>=0;i--)
{
// 循環到的當前敵人
tempEnemy = enemies[i];
// 如果當前敵人和自己發生碰撞
if(tempEnemy.hitTestObject(player))
{
chances--; // 生命-1
chancesText.text = chances.toString();
makeExplosion(player.x + (player.width / 2), player.y + (player.height / 2));
gameState = STATE_REMOVE_PLAYER;
}
}
}
// 創建並播放爆炸動畫
public function makeExplosion(ex: Number, ey: Number)
{
// 創建爆炸對象
var tempExplosion: MovieClip = new ExplosionImage();
tempExplosion.x = ex;
tempExplosion.y = ey;
this.addChild(tempExplosion);
// 由於爆炸動畫有7幀,會自動播放,所以放到爆炸數組里以后控制它們
explosions.push(tempExplosion);
// 播放爆炸聲音
var sound:Sound = new Explode();
sound.play();
}
// 查看等級(level)是否可增加或游戲是否可結束
public function testForEnd():void
{
if(chances <= 0)
{
removePlayer();
gameState = STATE_END_GAME;
}
else if(score > level * 30)
{
level++;
levelText.text = level.toString();
}
}
public function removeEnemy(idx:int)
{
this.removeChild(enemies[idx]);
enemies.splice(idx,1);
}
public function removeMissile(idx:int)
{
this.removeChild(missiles[idx]);
missiles.splice(idx,1);
}
public function removeExplosion(idx:int)
{
this.removeChild(explosions[idx]);
explosions.splice(idx,1);
}
public function onMouseDownEvent(e:MouseEvent)
{
if(gameState == STATE_PLAY_GAME)
{
// 創建子彈對象
var tempMissile: MovieClip = new MissileImage();
tempMissile.x = player.x + (player.width / 2);
tempMissile.y = player.y;
tempMissile.speed = 20;
missiles.push(tempMissile);
this.addChild(tempMissile);
var sound:Sound = new Shoot();
sound.play();
}
}
// 結束游戲
public function endGame():void
{
trace("游戲結束!");
}
}
}
