I need help understanding the difference between the event listener for keyboard input, versus
I simply want to hold left (H) to walk, and double-tap hold left to run.
I’ve added an event listener for a double tap left combo [H, H], to switch on a variable called dash
, and I have the function update()
checking for either left being pressed or left button being pressed + dash being active or not.
Here’s the code:
var dash = false;
function create ()
{
this.anims.create({
key: 'walk-left',
frames: this.anims.generateFrameNames('dx-motion', {
prefix: 'walk-',
suffix: '.png',
start: 1,
end: 32
}),
repeat: -1,
framerate: 32
});
this.anims.create({
key: 'run-left',
frames: this.anims.generateFrameNames('dx-motion', {
prefix: 'run-',
suffix: '.png',
start: 1,
end: 32
}),
repeat: -1,
framerate: 32
});
this.anims.create({
key: 'stand-left',
frames: this.anims.generateFrameNames('dx-motion', {
prefix: 'stand-',
suffix: '.png',
start: 1,
end: 23
}),
repeat: -1,
framerate: 32
});
keys = this.input.keyboard.addKeys({
left: 'H',
});
var combo2 = this.input.keyboard.createCombo(['H','H'], { resetOnMatch: true, maxKeyDelay: 600 });
this.input.keyboard.on('keycombomatch', function (event, keyEvent) {
if (keyEvent.code == 'KeyH'){
dash = true;
console.log(keyEvent.code);
console.log(dash);
}
});
} // End of function create()
and
function update ()
{
if (keys.left.isDown) {
dx_walker.setVelocityX(-160);
dx_walker.anims.play('walk-left', true);
if (dash == true && keys.left.isDown) {
dx_walker.setVelocityX(-310);
dx_walker.anims.play('run-left', true);
}
}
else {
dx_walker.setVelocityX(0);
dx_walker.anims.play('stand-left', true);
dash = false;
}
} // end of function update()
Here’s what it looks like.
The problem I have is that the run-left
animation is stuck on the sprite’s first frame. It should be playing all 32 frames of the run left animation. The physics behaves correctly but the dx_walker.anims.play('run-left', true);
is constantly starting again every update I guess.
Any ideas what I’m doing wrong?