SlimTim Posted February 13, 2014 Share Posted February 13, 2014 So, I'm looking through the keyboard docs in order to find a function for buttons being pressed as opposed to being held down. Say I'm firing a bullet, I only want one bullet spawned per button press, regardless of how long the button is held down. I have achieved this with raw JavaScript earlier using a boolean:isButtonADown = false;if(buttonA){ if(!isFiringButtonDown){ console.log("Shot fired."); isFiringButtonDown = true; }}else{ isFiringButtonDown = false;}Is there anyting like this implemented in Phaser? Link to comment Share on other sites More sharing options...
XekeDeath Posted February 13, 2014 Share Posted February 13, 2014 You can attach a function to the onDown event of a key.That will only fire once, no matter how long you hold the key down.var key = game.input.keyboard.addKey(Phaser.keyboard.keyCode); //see here: http://docs.phaser.io/Keyboard.js.html for the keycodeskey.onDown.add(function(key){ //fire in here}, this); Link to comment Share on other sites More sharing options...
Caio Wilson Posted September 16, 2014 Share Posted September 16, 2014 I'd say a better approach on this is:(I like prototyping better than throwing code around)//on create of YouGame.Game.prototype receiving game when declaredcreate: function () {//......gunfire = this.input.keyboard.addKey(Phaser.Keyboard.X);gunfire.onDown.add(this.gunfired,this);},//now the fire function after the update one:gunfired: function(key) {//fire it here//also you can attach a .name on gunfire and switch(key.name) for each type of gun.}, jdnichollsc 1 Link to comment Share on other sites More sharing options...
Recommended Posts