Jump to content

Question about keys: Pressed vs. isDown


SlimTim
 Share

Recommended Posts

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

  • 7 months later...

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.},
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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