Trying to simulate a gun shot

Hello, I’m trying to simulate the firing of a firearm in a 2D game, I managed to do this, but, the gun fires several times, is it possible to make 1 firing per 1 click of the mouse?

The code is basically (in update function):

this.pointer = this.input.activePointer;
if (this.pointer.leftButtonDown()) {
this.bullets.fireBullet(gunCoordinates, this);
}

Peek 2020-12-28 22-06

Use leftButtonReleased? Or a boolean that resets on button not down.

Hi @igorcfreittas,
Assuming you need the other mouse buttons, you can use a flag to determine when the shot is available:

// In create()
this.gunReady = true;
this.pointer = this.pointer.activePointer;


// In update()
if(this.pointer.leftButtonDown() && this.gunReady){
    this.bullets.fireBullet(gunCoordinates, this);
    this.gunReady = false;
}
if(this.pointer.leftButtonReleased() && !this.gunReady){
    this.gunReady = true;
}

If you don’t need the other mouse buttons then you could use the pointerdown event like in this example: http://labs.phaser.io/view.html?src=src\input\mouse\mouse%20down.js

Good luck!!

Thank you!

The code you passed helped me to fire 1 time per click, but I couldn’t fire it several times when I held the click, so I added the getDuration method and it started to work very close to what I want, it was like this:

if ((this.pointer.leftButtonReleased() || this.pointer.getDuration() >= 70) && !this.gunReady) {
this.gunReady = true;
}

Thanks for the suggestion, the final version was very similar to that!