Hi there!
I want to build a beat-based Jump’n’Run and have some problems with the jump. I use arcade physics with a gravity of “y: 0”, my player sprite is created with
this.sprite.body.setGravityY(1);
In my create()-function is a looped event like this:
create() {
this.bps = 600;
this.beat = this.time.addEvent({ delay: this.bps, callback: this.onEvent,
callbackScope: this, loop: true});
}
And an onEvent()-function like this:
onEvent () {
this.sound.play('beat');
console.log("Beat!");
// gravity
console.log(this.player.getJumping());
if(this.player.getJumping() == false) {
this.player.moveDirection("down");
}
}
So what I want to achieve is that every beat where the player isn’t jumping he should fall down. But when the player jumps, he should go up, stay there a bit and then go up even further (so the player can steer the jump left or right).
In my Player.js I have 3 functions which are relevant for the question (If you need more I’ll gladly provide):
jump() {
this.jumping = true;
this.sprite.body.setGravityY(0);
this.moveDirection("up");
setTimeout(() => {
this.moveDirection("up");
}, 600);
setTimeout(() => {
this.moveDirection("up");
}, 600);
setTimeout(() => {
this.jumping = false;
this.sprite.body.setGravityY(1);
}, 600);
}
getJumping() {
return this.jumping;
}
moveDirection(direction) {
// other directions [..]
} else if(direction == "down") {
this.sprite.body.setVelocityY(160)
setTimeout(() => {
this.sprite.body.setVelocityY(0);
}, 200);
}
}
Sometimes the jump behaves as wanted but most of the times the first or second part of the movement of the jump is short or cancelled. I think it’s because of the gravity setting in too soon, but I can’t figure out the right way to make it work like I want.
Thanks in advance to everyone who looks into the topic.