新手:我正在关注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’);
}
角色正在移动,但是当我们按下向上箭头键时,玩家将到达游戏屏幕的顶部,而当按下向下箭头时它将从顶部掉下。我尝试设置
player.body.gravity.y = 0;
仍然掉落或飞起来。左右移动是完美的,上下移动时我也需要类似的动作。 最佳答案
我想问题是您没有像这样清除velocity.y
函数中的update
:
function update() {
player.body.velocity.x = 0;
player.body.velocity.y = 0;
// below is your code from the question
}