======================================
[color=#CC0000]開始場景:[/color]
======================================
[color=#0033CC]stop[/color](); [color=#999999]// 停在最後一格[/color]
[color=#999999]//當「START GAME」按鈕被按下時, 即播放「遊戲」場景 (開始打老鼠)[/color]
StartBtn.[color=#0033CC]addEventListener[/color]([color=#0033CC]MouseEvent.CLICK[/color], startGame);
[color=#0033CC]function[/color] startGame([color=#0033CC]event:MouseEvent):void[/color] {
[color=#0033CC]gotoAndStop[/color](1,"[color=#66CC00]遊戲[/color]");
}
======================================
======================================
[color=#CC0000]遊戲場景:[/color]
======================================
[color=#0033CC]stop[/color]();
[color=#0033CC]Mouse.hid[/color]e();
stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
function MouseMove(event:MouseEvent):void {
catAttack.x=event.stageX;
catAttack.y=event.stageY;
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
function MouseDown(event:MouseEvent):void {
catAttack.gotoAndPlay("msDown");
RatHitTest(); //檢查是否打中老鼠
}
stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);
function MouseUp(event:MouseEvent):void {
catAttack.gotoAndPlay("msUp");
}
[color=#0033CC]var[/color] arrRat: [color=#0033CC]Array = new Array[/color](); [color=#999999]//陣列:用來儲存100隻老鼠物件[/color]
var arrIsAlive: Array = new Array(); [color=#999999]//陣列:用來記錄100隻老鼠是否活著[/color]
var KilledCount:int= 0; [color=#999999]//記錄打死了多少隻老鼠[/color]
var RatAddTimer:Timer=new Timer(300,100); [color=#999999]//產生老鼠的計時器[/color]
var RatRunTimer:Timer=new Timer(50,680); [color=#999999]//讓老鼠往上爬的計時器[/color]
RatAddTimer.start(); [color=#999999]//讓計時器開始計時[/color]
RatRunTimer.start();
[color=#999999]//每當第 1 個計時器警示時, 就要執行 RatAdd 函式來產生一隻老鼠[/color]
RatAddTimer.addEventListener(TimerEvent.TIMER, RatAdd);
function RatAdd(event:TimerEvent):void {
var rat1:MovieClip = new Rat()[color=#999999] //動態產生一隻老鼠物件[/color]
rat1.x = 20+ Math.random()*510; [color=#999999]//隨機產生一個 20~530 之間的水平位置[/color]
rat1.y = 410; [color=#999999]//垂直位置固定為 410 (舞台的底部再往下一點)[/color]
arrRat.push(rat1); [color=#999999]//將新產生的老鼠物件加入 arrRat 陣列中[/color]
arrIsAlive.push(true); [color=#999999]//將 true 值加入 arrIsAlive 陣列中, 表示還活著[/color]
addChildAt(rat1, 1); [color=#999999]//將新產生的老鼠物件加入舞台中, 如此才會顯示出來[/color]
}
[color=#999999]//每當第 2 個計時器警示時, 要讓每隻活的老鼠往上爬 12px[/color]
RatRunTimer.addEventListener(TimerEvent.TIMER,RatRun);
function RatRun(event:TimerEvent):void {
var i:int;
for (i = 0; i < arrRat.length; i++) { [color=#999999]// 分別處理陣列中每一個元素[/color]
if (arrIsAlive[i]) {
arrRat[i].y -= 12;
}
}
}
[color=#999966]//當第 2 個計時器依指定間隔與次數全部計時完成時, 要播放【結束】場景[/color]
RatRunTimer.addEventListener(TimerEvent.TIMER_COMPLETE, gotoEnd);
function gotoEnd(event:TimerEvent):void {
gotoAndPlay(1,"結束"); [color=#999966]//播放【結束】場景的影格 1[/color]
}
function RatHitTest():void { [color=#999999]//檢查是否打中老鼠[/color]
var i:int;
for (i = 0; i < arrRat.length; i++) {
if (arrIsAlive[i]) {
if (catAttack.hitTestObject(arrRat[i])) { [color=#999999]//採用比較寬鬆的比對方式[/color]
arrIsAlive[i] = false; [color=#999999]//設為已死[/color]
arrRat[i].gotoAndPlay("killed"); [color=#999999]//播放老鼠的死狀[/color]
KilledCount += 1; [color=#999999]//將命中老鼠的計數器加 1[/color]
}
}
}
}
======================================
======================================
[color=#CC0000]結束場景:[/color]
======================================
Mouse.show(); [color=#999966]//將滑鼠指標顯示出來[/color]
[color=#999999]//移除我們在「遊戲」場景中自訂的按下滑鼠事件函式[/color]
stage.removeEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
stage.removeEventListener(MouseEvent.MOUSE_UP, MouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, MouseMove);
[color=#999999]//設定當按下【REPALY?】鈕時, 即播放「遊戲」場景重開戰場[/color]
PlayAgain.addEventListener(MouseEvent.CLICK, gotoStart);
function gotoStart(event:MouseEvent):void{
gotoAndStop(1,"遊戲");
}
numKilled.text = String(KilledCount); [color=#999999]//將命中計數的值存入 numKilled 動態文字[/color]
[color=#999999]//依命中的多寡給予評分[/color]
if (KilledCount >= 90) {
comment.text = "我果然寶刀未老,喵~~";
} else if (KilledCount <= 50) {
comment.text = "太久沒運動了,喵嗚~~";
} else {
comment.text = "喵喵我太輕敵了!再來一局吧!讓我好好大顯身手!";
}
stop(); [color=#999999]// 停在此影格[/color]
======================================
[color=#CC0000]被打老鼠元件[/color]
======================================
stop();
parent.removeChild(this); //把自己移除掉
======================================
======================================
[color=#CC0000]嚴格的語法[/color]
======================================
if (catAttack.hitTestPoint(arrRat[i].x, arrRat[i].y,true)) { //採用比較嚴格的比對方式
======================================
Edited 5 time(s). Last edit at 12/23/2010 04:02PM by JinJin.
(
編輯記錄)