Reputation: 191
I'm new to Phaser 3, and I'm trying to make my character jump.
Here is my code:
create() {
this.kid = this.physics.add.sprite(50, 380, 'idle');
this.kid.setScale(.3);
this.kid.setGravityY(300);
this.kid.setBounce(0.2);
this.kid.setCollideWorldBounds(true);
this.physics.add.collider(this.kid, this.platforms);
this.cursorKeys = this.input.keyboard.createCursorKeys()
}
update() {
this.moveKid()
}
moveKid() {
if (this.cursorKeys.left.isDown) {
this.kid.setVelocityX(-300)
} else if (this.cursorKeys.right.isDown) {
this.kid.setVelocityX(300)
} else {
this.kid.setVelocityX(0);
this.kid.setVelocityY(0);
}
if (this.cursorKeys.up.isDown && this.kid.body.touching.down) {
this.kid.setVelocityY(-300);
}
}
But currently, the character only jumps a few pixels up and thats all. If I remove the touching.down
part, the player jumps freely but also jumps in the air and falls very, very slow (no matter the gravity I set to it to).
Could anyone help?
Upvotes: 0
Views: 1434
Reputation: 442
You would want to remove the line in your code where you set the character's y velocity to zero whenever there's no keyboard input. It stops the character's motion every time the game updates, hence the slow fall speed.
Upvotes: 3