移动设备上的活动指针问题,使用 Phaser 3
Issue with active pointer on mobile, with Phaser 3
我试图通过点击游戏的右侧或左侧来移动我的玩家。虽然在电脑上一切正常,但当我在手机上尝试时,它总是向右移动,经过多次尝试后,无论按哪里,它都开始向左移动。这是我的 update
函数:
if (game.input.activePointer.isDown)
{
if (game.input.mousePointer.x > 600)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else if (game.input.mousePointer.x < 200)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
} else {
player.setVelocityX(0);
player.anims.play('turn', true);
}
那是因为您使用的 mousePointer
只会查看鼠标。
来自the mousePointer documentation:
The mouse has its own unique Pointer object, which you can reference directly if making a desktop specific game. If you are supporting both desktop and touch devices then do not use this property, instead use activePointer which will always map to the most recently interacted pointer.
为您的所有逻辑切换到 activePointer
,您应该已设置好。
如果您正在使用物理学(看起来您可能是这样),那么它轻微移动的原因可能与您代码中其他地方的逻辑有关;考虑到您的 if
陈述,这是我能想到的唯一原因。
我试图通过点击游戏的右侧或左侧来移动我的玩家。虽然在电脑上一切正常,但当我在手机上尝试时,它总是向右移动,经过多次尝试后,无论按哪里,它都开始向左移动。这是我的 update
函数:
if (game.input.activePointer.isDown)
{
if (game.input.mousePointer.x > 600)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else if (game.input.mousePointer.x < 200)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
} else {
player.setVelocityX(0);
player.anims.play('turn', true);
}
那是因为您使用的 mousePointer
只会查看鼠标。
来自the mousePointer documentation:
The mouse has its own unique Pointer object, which you can reference directly if making a desktop specific game. If you are supporting both desktop and touch devices then do not use this property, instead use activePointer which will always map to the most recently interacted pointer.
为您的所有逻辑切换到 activePointer
,您应该已设置好。
如果您正在使用物理学(看起来您可能是这样),那么它轻微移动的原因可能与您代码中其他地方的逻辑有关;考虑到您的 if
陈述,这是我能想到的唯一原因。