AndreasWienes Posted April 8, 2014 Share Posted April 8, 2014 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 bestAndreas Link to comment Share on other sites More sharing options...
ctmartinez1992 Posted April 8, 2014 Share Posted April 8, 2014 That doesn't work? It looks like a possible solution... You can also disable the keyboard, if it helps:cursors = game.input.keyboard.disable = false; Link to comment Share on other sites More sharing options...
AndreasWienes Posted April 8, 2014 Author Share Posted April 8, 2014 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 comment Share on other sites More sharing options...
rich Posted April 9, 2014 Share Posted April 9, 2014 Key.enabled was added in 2.0.3 which will be released later this week, in the meantime the best way is probably to remove and add back in your key event handlers, rather than the key objects themselves (i.e. onDown.add(handler)). Link to comment Share on other sites More sharing options...
AndreasWienes Posted April 9, 2014 Author Share Posted April 9, 2014 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 comment Share on other sites More sharing options...
Heppell08 Posted April 9, 2014 Share Posted April 9, 2014 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 comment Share on other sites More sharing options...
AndreasWienes Posted April 9, 2014 Author Share Posted April 9, 2014 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 comment Share on other sites More sharing options...
rich Posted April 9, 2014 Share Posted April 9, 2014 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. AndreasWienes 1 Link to comment Share on other sites More sharing options...
Heppell08 Posted April 9, 2014 Share Posted April 9, 2014 Here is a post I created and ended up answering myself too. I explained in there how I use my paused. Basically a true/false but with numbers instead.http://www.html5gamedevs.com/topic/4353-pausedunpause-issue/ AndreasWienes 1 Link to comment Share on other sites More sharing options...
AndreasWienes Posted April 9, 2014 Author Share Posted April 9, 2014 Thanks Heppell08 your advice helped me. As you said, I declare a global variable "paused" and before checking if a key is pressed, I test if "paused" is true or "false". Problem solved. :-) Link to comment Share on other sites More sharing options...
Recommended Posts