简体   繁体   中英

Pause the game (ActionScript 3)

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 background.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM