2011-08-01 126 views
-1

我寫了遊戲。它在運行程序時立即啓動。一切工作在一個文檔類中。現在,我想做一些基本的介紹,例如遊戲開始前的動畫倒計時。我怎麼能暫停遊戲?主時間軸上只有一幀包含背景。暫停遊戲(ActionScript 3)

+0

在遊戲代碼開始前放入一個計時器。在獲得特定答案之前,您需要使用代碼示例提出更詳細的問題 – shanethehat

+0

如何讓文檔類在第二個或其他框架上啓動? – nicks

+1

你不知道。您不必讓代碼開始遊戲,而是讓代碼啓動您的計時器動畫,並在遊戲完成時纔開始遊戲。如果你發佈你的代碼,也許你會得到一個更清晰的答案。 – shanethehat

回答

4

如果您的動畫基於計時器。

當啓動定時器:

timer.start(); 
last_time = getTimer(); 

時暫停計時器:

timer.stop(); 
pause_timer = getTimer() - last_time; 

時恢復定時器:

last_time = getTimer(); 
timer.start(); 

希望,它會幫助你。

2

要添加到上面的Antony的答案,如果您使用事件偵聽器來處理遊戲循環操作,您可以簡單地刪除它們以暫停遊戲,然後再次添加它們以重新啓動它。例如:

package com.mygame.logic{ 
import flash.display.MovieClip; 
import flash.display.Bitmap; 
import fl.controls.Button; //to get this code to work you have to drag a UI component to your 
//movie's library or Flash won't recognize it. 
public class mygame extends MovieClip{ //this is to be the main document class for the .fla 
private var bmp:Bitmap = new Bitmap(...); //fill in constructor with relevant data 
private var myButton:Button = new Button(); 
private var paused:Boolean = false; 
public mygame(){ 
    bmp.x = 100; 
    bmp.y = 100; 
    myButton.x = 200; 
    myButton.y = 200; 
    this.addChild(bmp); 
    this.addChild(myButton); 
    this.addEventListener(Event.ENTER_FRAME, main); 
    myButton.addEventListener(MouseEvent.ON_CLICK, pause); 
} 
public function main(e:Event):void{ 
    bmp.x += 1.0; 
} 
public function pause(e:MouseEvent):void{ 
    if (!paused){ 
    this.removeEventListener(Event.ENTER_FRAME, main); 
    this.paused = true; 
    } 
    else{ 
    this.addEventListener(Event.ENTER_FRAME, main); 
    this.paused = false; 
    } 
} 

並且應該爲基本的暫停功能做。你可以擴展上述做出一個很好的平視顯示器用於命名和彩色按鈕等用於暫停/重新啓動遊戲的玩家,使用補間來使HUD過渡到屏幕上很好...

希望它可以幫助, CCJ