Monitor key "down-up" sequence

Hi guys, I have a cannon I want to fire, but with a “held down” mechanic for shot power. What I want to know is whether the “fire cannon” key was pressed and then released (let’s leave the duration part for another post).

This is what I wrote:

fireCannon(){
        const keyFireCannon = this.scene.input.keyboard.addKey(FIRE_CANNON);
        if (keyFireCannon.isDown){
            keyFireCannon.on('keyup', function(){
                //const shotAngle = keyFireCannon.duration;
                if (this.activeCannon == CANNON_FRONT_ID){
                    positionMuzzleBlast();
                    this.muzzleBlast.anims.play('muzzleBlast');
                }
                
            });
        }
    }

But when I press the key, no animation is displayed. All I want to achieve now is for the animation to display.

Hi @Tico1711,
Without more context, I wouldn’t know if your code has a problem. But I gather that you are calling fireCannon () from the update (), and therefore creating a listener every time the key is pressed (that’s a lot of times).

This would be a different way to detect the “down-up” and get the time that a key was pressed:

See the Pen [Phaser 3] Time pressing a key by Juan Jose Capellan (@jjcapellan) on CodePen.

I hope you find it useful.
Regards.

1 Like

Life saver! I am indeed calling that from the UPDATE. Will look into your example in detail to implement my solution for this.

Thank you!