开发者

Pause the game (ActionScript 3)

开发者 https://www.devze.com 2023-03-24 10:44 出处:网络
I have written the game. It starts at once, when running the program. EVERYTHING works in one Document Class. Now, I want to do some basic intro, for example, animated countdown before the game starts

I have written the game. It starts at once, when running the program. EVERYTHING works in one Document Class. Now, I want to do some basic intro, for example, animated countdown before the game starts. How can I kinda pause the game? there is only one frame on the main timeline which contains the backgroun开发者_StackOverflow社区d.


If your animation based on the timer.

When start the timer:

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

when pause the timer:

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

when resume the timer:

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

Hope, it will helps you.


To add to Antony's answer above, if you are using event listeners to handle the game loop actions, you can simply remove them to pause the game and then add them again to re-start it. For example:

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;
  }
}

and that should do it for a basic pause function. You can expand upon the above to make a nice HUD for the player with nicely named and colored buttons etc. for pausing/restarting the game, use tweens to make the HUD transition onto the screen nicely...

hope it helps, CCJ

0

精彩评论

暂无评论...
验证码 换一张
取 消