Skip to main content

打打打爆小人頭!! 我的第一個FL1.1小遊戲 part 4

2. 計時器與重覆使用的程式:
FL1.1的Call(frame label):
Flash 4沒有function寫法,只有frame action,要重複利用的code是寫在一個無關的frame上,利用它的frame label來執行Call(frame label)指令模擬function。所以接下來主遊戲程式使用到的重複code,都將寫在一個timerFunc MovieClip中。


"timerFunc" MovieClip:
[圖10] 時間軸上配置,前三格為計時器程式後面留給Call()使用(善用空間..=P)

Frame 1:
timer = getTimer();
//getTimer()重出江湖嚕!^^,先抓取timer起始值


Frame 2:
if ((getTimer()-timer)>=1000) {
//當getTimer抓到一秒時,就回到frame1重新抓起始值;
if (_root.sec<=0) {
tellTarget ("/") {
gotoAndStop("end");
//遊戲結束
}
_root.end_txt = "You got total\n" add _root.score add " heads!!";
//string相加一定要使用add代替+
call("pause");
//將動畫停止
stop();
} else {
_root.sec--;
//減秒
gotoAndPlay(1);
}
}


Frame 3:
gotoAndPlay(2);
//不滿一秒就是重複回到frame 2


這樣一個簡單的計時器程式就完成了~~
接下來處理重複利用的程式:

Frame label 「beHit」: 判斷小人頭有無被打,會傳入hitNum告知被打號碼
myName = "head_mc_" add hitNum;
if (_root[myName].isActive) {
tellTarget ("/" add myName) {
gotoAndPlay("beHit");
//爆血動畫
}
_root.score++;
}


Frame label 「pause」: 暫停遊戲,由於小人頭動畫有特別簡化,所以可以很方便的利用簡短的程式將所有動畫停止
for (i=1; i<10; i++) {
myName = "head_mc_" add i;
if (_root[myName].isActive) {
//有動作的小人頭才要stop
tellTarget ("/" add myName) {
stop();
}
}
}
tellTarget ("/headsControler") {
gotoAndStop(1);
}
stop();
//這個stop是指計時器迴圈stop喔!


Frame label 「replay」: 判斷小人頭有無被打,會傳入hitNum告知被打號碼
for (i=1; i<10; i++) {
myName = "head_mc_" add i;
if (_root[myName]._currentframe != 1) {
//有動作的小人頭才要繼續play
tellTarget ("/" add myName) {
play();
}
}
}
tellTarget ("/headsControler") {
play();
}
play();
//這個play是指計時器迴圈play喔!


現在可以先測試Movie去看看倒數計時器有沒有效果囉!

to be continued...

Comments

  1. 怎麼不貼連結在MMUG呢?
    Flash on Mobile Device需要這樣的好文.

    ReplyDelete

Post a Comment

Popular posts from this blog

PureMVC 我也會 [6]

Mediator ViewComponents 與 pureMVC 架構的中介 監聽並反應 View Component 發出的 Event 可以發送與接收 Notification 儘量少操作 Proxy 公開方法,多用 sendNotification... Mediator design pattern 要多認識這個 Mediator 設計模式的話,請自行看連結說明啊! 簡單來講,假使有一個 View 裡面有好幾個 MovieClip 組成,而這些 MovieClip 會互相影響對方...這個情況在 Flash 中,通常都會變成下圖: MovieClip 直接控制其他 MovieClip 搞到整個關係很複雜...換一個元件簡直是災難。 加入 Mediator 後,示意圖就會變成: 這樣,所有的 MovieClip 都透過 Mediator 來跟其他 MovieClip 溝通,當某一個 MovieClip 替換成別的元件,這時候也只需要修改 Mediator 中的引用即可,是不是變得很乾淨?如果同一組 MovieClip 有另外一個操作模式,也只需要替換掉 Mediator 即可!天下太平啊~~~ 而 PureMVC 中就是利用 Mediator class 為與前端 ViewComponent 的中介,這樣可以切開 ViewComponent 與 PureMVC framework 的關係,不管你前端介面使用 Flash or Flex 製作都跟程式核心無關。 所以 ViewComponent 製作時只需要兩個原則,一把所有的請求都以 Event 送出由 Mediator 處理,二提供公開方法, Mediator 只需要監聽 View 的 Event,將收到的資訊透過公開方法喂進 ViewComponent 即可。 如在 ViewComponent 中: public function setList( result:Object ):void{ list.dataProvider = result as ArrayCollection; } //然後在按下取得資料的按鈕 Click action 寫上: dispatchEvent( new Event( "GET_LIST" )); 新建 Mediator 的時候一樣有幾個重點方...