Skip to main content
October 29, 2011
Answered

How to pause a Timer??

  • October 29, 2011
  • 1 reply
  • 4264 views

Im finishing of one of my many projects, I HATE UI!! and i read about 7 things to avoid on how to make a crappy game, and one of them was a pause button , i managed to add all the other things in, but im absolutely stumped on a pause button. I read one tutorial, which didn't really help. it was talking about a main game loop and all that crap that just baffled me. i did try it though, and i did not get any errors, just ALOT of laf, becaus what i did was put all my constructor code into the 'update' function, so it was running my spawn enemies every frame ect. but the i realised all i need to do is pause my timer, because thats the only thing that really needs to be done right? i read somewhere about pausing timers with getTimer, but that confused me as no where was really explaining the concept just giving the answer really. Can someone help me please ??

here is my timer:

public function setCrates()

                    {

                              DropCrate = new Timer(10000+Math.random()*10000,1);

                              DropCrate.addEventListener(TimerEvent.TIMER_COMPLETE, newCrate);

                              DropCrate.start();

                    }

                    public function newCrate(Event:TimerEvent)

                    {

                              var StartPosition:Number = Math.random() * 90 + 10;

                              // create plane

                              var c:Crate = new Crate(StartPosition);

                              addChild(c);

                              Crates.push(c);

                              setCrates();// set time for next crate;

                    }

this is the game loop thing i was talking about:

package

{

import flash.display.MovieClip;

import flash.events.Event;

public class Main extends MovieClip

{

private var isPaused:Boolean; // This is the variable that holds our paused/unpaused state

public function Main()

{

// Unpause the game:

isPaused = false;

// Add an event listener to keep an eye (ear?) on the built-in ENTER_FRAME event.

// Every time Flash processes a frame tick, doMasterGameLoop will execute:

addEventListener(Event.ENTER_FRAME, doMasterGameLoop);

}

private function doMasterGameLoop(e:Event = null):void

{

// This is the main game loop that does all the work.  It's the

// "gateway" to everything our game does.  If the game is paused,

// nothing in the game loop happens.

if (!isPaused)

{

update();

draw();

}

}

private function update():void

{

// Tweak all your numbers and math stuff here

}

private function draw():void

{

// Update all your graphics here

}

}

}

This topic has been closed for replies.
Correct answer

No, you can restore "flash.utils.Timer" as it was before.

You call the pause() function just as you call the moveEnemy() function. I wrote "enemy.pause()", not "Enemy.pause()" and I ment an instance of enemy, I don't know where do you keep them

Forexample if you had an array called enemies, the code would look like this:

private function pauseEnemies():void {

     for(var i:int = 0; i < enemies.length; i++) {

          var enemy:Enemy = enemies as Enemy;

          enemy.pause();

     }

}

You need a references to the enemies. Where do you create enemies? Paste a code.


Here is what im using to make my enemy's:

                         public function setNextEnemy()

                    {

                                nextEnemy = new Timer(1000+Math.random()*1000,1); // I replaced the PausableTimer with this one again

                              nextEnemy.addEventListener(TimerEvent.TIMER_COMPLETE,newPlane);

                              nextEnemy.start();

                    }

                    public function newEnemy(event:TimerEvent)

                    {

                              // im spawing the enemies from a random side of the stage

                              if (Math.random() > .5)

                              {

                                        var side:String = "left";

                              }

                              else

                              {

                                        side = "right";

                              }

                              var altitude:Number = Math.random() * 90 + 10;

                              var speed:Number = Math.random() * 100 + 150;

                              // create plane

                              var e:Enemy = new Enemy(side,speed,altitude);

                              addChild(e);

                              Enemies.push(e);

                              setNextEnemy();// set time for next plane;

                    }

1 reply

Ned Murphy
Legend
October 29, 2011

The Timer class has start() and stop() methods that you can use as needed.  If you have any wonderings about other features, just look up the class in the help docs.

October 29, 2011

thank you for your reply ned. Yes i know i can stop() the timer, which i do at the end of the level as such. But i need to be able to pause it and resume it on a button, incase the user needs to do something or just simply wants to take a break. i apoligise if i am being unclear.

Peter Celuch
Legend
October 29, 2011

To pause timer you should use timer.stop(), to resume use timer.start().

If you ment that you want the timer to take less time to complete once paused / resumed, you'll need another variable, let's say "timeLeft". Where you will store time left when pausing and you will pass its value to timer when you resume it.

You can extend Timer class and create your own, pausable Timer.