Jump to content

Disable particular keys while game is paused


Go to solution Solved by AndreasWienes,

Recommended Posts

Hey guys,

 

I'm trying to code a pause function into my little game. I already read the other treads about this topic. I found no way to disable only particular keys, like the cursors, while game is paused. 

 

My code:

pauseGame: function() {        pauseSound.play();        this.paused = !this.paused;        if (this.paused) {            level.pause();            player.pause();        }        else {            level.resume();            player.resume();        }    }

And inside Player.js:

pause: function()  {        this.game.input.keyboard.removeKey(Phaser.Keyboard.UP);    },        resume: function() {        this.game.input.keyboard.addKey(Phaser.Keyboard.UP);            }

Thanks for your help!

 

All the best

Andreas

 

Link to post
Share on other sites

Thanks for your help but both attempts unfortunately don't work for me. 

Maybe 

game.input.keyboard.removeKey(Phaser.Keyboard.UP);game.input.keyboard.addKey(Phaser.Keyboard.UP);

is only working inside the create function?

removeKey performs correctly, but adding shows no reaction.

 

Disabling the whole keyboard, won't work, just because I need the "P"-key to reactivate the game.

Link to post
Share on other sites

Thank you Richard. Till now I used the createCursorKeys() function and checked if one of those keys is pressed inside the update function. So no event handlers have been used. After your hint I changed my code as follows:

 

 

create: function() {                this.cursors = this.game.input.keyboard.createCursorKeys();        this.cursors.left.onDown.add(this.moveLeft, this);                    },    moveLeft: function() {        this.jumpSound.play();        this.sprite.body.velocity.x = -150;            }

But from my point of view, the movement of the sprite is not happening in the update function when using the event handler. The sprite moves, but I need to press the key for each movement.

Link to post
Share on other sites

Make a global variable and set it to 0. Then when the game is paused set it to 1. If the variable === 1 just disable with that. Then in unpause reset the variable value back to 0 and initiate the keys again. Its how I did it in my own rolled paused function. Also may be useful I use 1.1.5 but that method can work outside of any phaser versioning.

Link to post
Share on other sites

Hello Heppell08,

 

This is what I'm doing:

 

myGame.Game = function(game) {    var paused = false;};
myGame.Game.prototype = {        create: function() {        pauseKey = this.game.input.keyboard.addKey(Phaser.Keyboard.P);        pauseKey.onDown.add(this.pauseGame, this);    },        pauseGame: function() {        pauseSound.play();        this.paused = !this.paused;        if (this.paused) {            level.pause();            player.pause();        }        else {            level.resume();            player.resume();        }    }}

And inside Player.js:

 
pause: function()  {        // this.game.input.keyboard.removeKey(Phaser.Keyboard.UP);        // cursors = this.game.input.keyboard.disable = true;            },

Could you please explain further what exactly you meant saying "just disable with that" or show me your solution with some code?

Thanks a lot for your help!

 

Link to post
Share on other sites

 

Thank you Richard. Till now I used the createCursorKeys() function and checked if one of those keys is pressed inside the update function. So no event handlers have been used. After your hint I changed my code as follows:

 

 

create: function() {                this.cursors = this.game.input.keyboard.createCursorKeys();        this.cursors.left.onDown.add(this.moveLeft, this);                    },    moveLeft: function() {        this.jumpSound.play();        this.sprite.body.velocity.x = -150;            }

But from my point of view, the movement of the sprite is not happening in the update function when using the event handler. The sprite moves, but I need to press the key for each movement.

 

 

Agreed - don't do it like this for player movement. Just wait until 2.0.3 lands in a day or two.

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.

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

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...