2011-08-01 127 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