我正在用 Phaser 制作游戏。当我的播放器向左或向右移动时,它会在停止之前继续移动一段时间。有什么办法可以阻止这种情况以获得更灵敏的运动吗?
代码是这样的:
if(cursors.left.isDown){
player.body.velocity.x += -20;
}
最佳答案
如果您想立即停止播放器
,只需将player.body.velocity.x
设置为0即可。
player.body.velocity.x = 0;
一些官方教程,比如这个Mass Velocity Test举个例子。在这种情况下,他们实际上在 update() 调用开始时将 x 和 y 速度重置为 0。这允许立即停止和启动。
如果由于某种原因你想要加速但立即停止,你可能只使用 else。
if (cursors.left.isDown) {
player.body.velocity.x += -20;
} else {
player.body.velocity.x = 0;
}
您还可以将速度存储在变量中,然后在 if 语句中使用它。未经测试:
currentVelocity = player.body.velocity.x;
player.body.velocity.x = 0;
if (cursors.left.isDown) {
player.body.velocity.x = currentVelocity - 20;
}
关于javascript - 移动时阻止玩家滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37062383/