caesar getit
caesar getit

Reputation: 225

How to set keyboard key pressed event only once after pressed but not continously

I am using below code to move the player up, but the problem is as long as the button is pressed and held the player continues to move. How can I change this behaviour so that no matter how much time the button is pressed for the player moves only once?

if (cursor.up.isDown){
     player.body.velocity.y = -200;
     player.animations.stop('move');
}

Upvotes: 3

Views: 5189

Answers (3)

scope
scope

Reputation: 61

I know this an old post but i got stuck here too and used your example to come up with this nice and simple solution

this.speed = 100
this.player.setVelocity(0,0)
if (this.cursors.left.isDown){
  this.player.setVelocityX(-this.speed)
}else if(this.cursors.right.isDown){
  this.player.setVelocityX(this.speed)
}else{
  this.player.setVelocityX(0)
}if(this.cursors.up.isDown){
  this.player.setVelocityY(-this.speed)
}else if (this.cursors.down.isDown){
  this.player.setVelocityY(+this.speed)
}else{
  this.player.setVelocityY(0)
}

First we set the players velocity to 0 , 0 (both x and y). We check for any changes on the x axis else reset that back to 0. Check on the y axis for changes else reset that back to 0 also.

This i found allows the character to move in a more fluid motion. and also allows for both left and up etc to be pressed together. if you need the character to move more square like then change velocity to .x or .y

Upvotes: 0

Moisés
Moisés

Reputation: 1494

Kamen Minkov answer does work, but if your idea is to make a jump the bool will not work, as soon you realese the button you can press it agan and go further up, even not touching any ground.

But you can use this function to verify if the body is touching down

function touchingDown(someone) {
var yAxis = p2.vec2.fromValues(0, 1);
var result = false;
for (var i = 0; i < game.physics.p2.world.narrowphase.contactEquations.length; i++) {
    var c = game.physics.p2.world.narrowphase.contactEquations[i];
    if (c.bodyA === someone.data || c.bodyB === someone.data)        {
        var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis
        if (c.bodyA === someone.data) d *= -1;
        if (d > 0.5) result = true;
    }
} return result;
}

and call sending the body

if ( (cursor.up.isDown) && (touchingDown(player.body)) ){
        player.body.velocity.y = -200;
        player.animations.stop('move');
}

OBS: function for P2 Physics, but for arcade the body already have a field that says if it is touching down.

Upvotes: 1

Kamen Minkov
Kamen Minkov

Reputation: 3722

A bool acting as a flip-flop switch should do the job:

var flipFlop;

function update() {
    if (cursor.up.isDown){
        if (!flipFlop) {
            player.body.velocity.y = -200;
            player.animations.stop('move');
            flipFlop = true;
        }
    }

    if (cursor.up.isUp) {
        flipFlop = false;
    }
}

Note that the flipFlop variable is declared outside the update loop, otherwise it will be recreated every frame.

Upvotes: 4

Related Questions