简体   繁体   中英

How to make up and down motion in phaser js without gravity

Newbie: I am following this tutorial

I tried adding up and down motion also like:

if (cursors.left.isDown) {
    //  Move to the left
    player.body.velocity.x = -150;
    player.animations.play('left');
} else if (cursors.right.isDown) {
    //  Move to the right
    player.body.velocity.x = 150;
    player.animations.play('right');
}else if (cursors.up.isDown) {
   // Move to the top
   player.body.velocity.y = -50;
   player.animations.play(‘top’);
} else if (cursors.down.isDown) {
   // Move to the bottom
   player.body.velocity.y = 50;
   player.animations.play(‘bottom’);
}

The character is moving but when we press up arrow key, player will go till top of the game screen, and when down arrow is pressed it will fall from top. I tried setting player.body.gravity.y = 0; still it falls down or flies up. Lelt and right motion is perfect, I need a similar behavior when moving up or down.

I guess the problem is that you're not clearing velocity.y in your update function like this:

function update() {
    player.body.velocity.x = 0;
    player.body.velocity.y = 0;
    // below is your code from the question
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM