javascript - 如何在无重力的情况下在phaser js中进行上下运动

标签 javascript phaser-framework

新手:我正在关注这个tutorial

我尝试添加上下运动,例如:

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’);
}

Angular 色正在移动,但是当我们按下向上箭头键时,玩家将到达游戏屏幕的顶部,而当按下向下箭头时,它将从顶部掉落。我尝试设置player.body.gravity.y = 0;它仍然会掉下来或飞起来。左右运动是完美的,向上或向下移动时我需要类似的行为。

最佳答案

我猜问题是您没有在 update 函数中清除 velocity.y,如下所示:

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

关于javascript - 如何在无重力的情况下在phaser js中进行上下运动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25222143/

相关文章:

javascript - 添加 YYYY、MM、DD 下拉列表以在网页上选择日期

javascript - 使用 tileset 将 map 更改为另一个 map

javascript - 如何将字符串循环为键值对

javascript - 在一个导航下有多个 slider

javascript - 有没有办法在 'CallBack' 自定义 `jQuery` 中设置 `trigger` ?

javascript - 在 React 中包含 Phaser JS,PIXI 未定义错误

javascript - 移相器,流体扫射(对 Angular 线输入)

javascript - 通过两种不同的方法将数据从 HTML 发送到 Servlet

javascript - Phaser 将物理应用到 Phaser.Sprite (ES6) 的子类

phaser-framework - 使用移相器更改 Sprite 的色调