Jump to content

Space bar listener to trigger pause screen only firing twice

Go to solution Solved by jpdev,

Recommended Posts

I have a listener on the space button the pause the game, like so:

this.space = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);this.space.onDown.add(function() {    game.paused = !game.paused;}, this);

I press the spacebar once, it pauses the game. I press it again, it unpauses it. I press it AGAIN and it doesn't pause. Why is this happening?


Also; if I replace the 

game.paused = !game.paused;

with just 


I get a log at every press, like desired. So it has something to do with the pause mid-execution of the listener. Not sure what the fix for that is, though.

Link to post
Share on other sites
  • Solution

Is it possible that there is more going on in your code?

Because i just pasted your 4 lines from the post directly into the create function of my game, and space paused and unpaused the game multiple times without problems.


(disclaimer: the project I just checked this in is phaser 2.0 beta .. not the current 2.0.3)

Link to post
Share on other sites

I think using dev directly is a gamble. (things might be improved, other things might be temporary broken.)


It also depends on if you build the phaser.js (and phaser.min.js) from the content of the dev repository, or if you use the phaser.js that is in the dev repository.


(For building you need nodejs and grunt - it's not hard to do, but takes at least some getting used to - i am sure there is a tutorial how to do it somewhere *g*)


This is because more often then not (rich and other contributors) just updates the single source files and not always the complete phaser.js.

Link to post
Share on other sites

Cool. I have a new problem though... Here's my code now:


this.space.onDown.add(function() {    if(!game.paused) {        console.log('Here.');        this.g = this.game.add.graphics(208, 126);        this.g.beginFill(0xffffff, 64);        this.g.drawRect(new Phaser.Rectangle(0, 0, 720, 388));        this.g.endFill();        game.paused = true;    } else {        this.g.destroy();        game.paused = false;    }}, this);

Unfortunately the graphics is never drawn  :(


Wait a second I messed up, I see what I did wrong. I should be drawRect(new Phaser.Rectangle(0, 0, 720, 388))

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...