简体   繁体   English

如何在没有重力的情况下在Phaser JS中进行上下运动

[英]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; 我尝试设置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: 我想问题是您没有像这样在update函数中清除velocity.y

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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