Jump to content

How to pause and resume a timer


AVAVT
 Share

Recommended Posts

I have a Phaser.Timer created with this snipplet to update the score based on elapsed time and create a new enemy regularly:

scoreTimer = game.time.create(game);scoreTimer.loop(100, updateScore); //update score based on elapsed timescoreTimer.loop(1000, createEnemy); // create new enemyscoreTimer.start();// According to the docs this part is not neededgame.onPause.add(function(){    this.pause();}, scoreTimer);game.onResume.add(function(){    this.resume();}, scoreTimer);

There are... many weird... behaviors I didn't expect here:

 

When I pause the game (thus pause the timer), everything seems to work fine, but when I resume the game, nothing happens for several seconds (no new enemy is created, and the score stays the same). And then suddenly when a new enemy is created, the score jumps to a far ahead number as if the timer has been running for the whole time. Example:

 

1. Every second the player gain 1 score. I pause the game when I'm having 3 score then resume it instantly.

2. The score text stay at 3 for 5 seconds and during that 5 seconds no new enemy is created.

3. After that a new enemy is created and the game behaves normally, except that the score text show 8 point (3 + 5).

 

Here's my updateScore function:

function updateScore(){    score = scoreTimer.seconds.toFixed(1);    scoreText.content = 'score: ' + score;}

What I want to do is:

- When I pause the game, everything freeze, nothing moves, nothing is created, no score is gained. It just freezes, coldly, like frozen ice, or my home PC every time I try to do something productive.

- When I resume the game, everything continues on from where it has been. Enemies keep on coming, score goes from 3 to 4, etc.

 

Could anybody tell me what did I do wrong?  I'm totally new to creating game, just trying to swallow slowly, step by step how to make a simple game, but the docs is so... full of uninformative information I feel lost :(

 

 

Also another question to illustrate my meaning of uninformative information:

 

I was trying to make the score text to the top of the display stack (highest z-index if you know what I mean).

 

I couldn't find something similar to a z-index in the docs (if there is, please do tell me) so I thought about bringing the text to top in my render function.

 

I found Sprite.bringToTop(), and the docs say:

 

Brings the Sprite to the top of the display list it is a child of. Sprites that are members of a Phaser.Group are only bought to the top of that Group, not the entire display list.

 

 

I'm stuck from here. It's very nice that the docs tell me this function can't do what I want, but it doesn't help if I don't know what function can do what I want, is it?

 

 

 

Finally, could anybody show me where can I lookup for things like "what's in side the initial game object", "what's game.time"? Better yet, how do I know what is the purpose of a class/object (what the creator intended it to be), and how to use it?

 

Right now I still don't understand why new Timer(game, true); doesn't work, but game.time.create(game); does, I just copied the code from a google search result and it runs. I don't even know how am I supposed to conjure out these kinds of code next time it happens >_<

 

 

I'm really sorry I'm normally not this dumb (and lazy), but I'm trying to learn both how to create a game and how to use Phaser at the same time. The progress has been terribly slow and it's sapping my mind and my willpower :(

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...