我正在尝试通过拖动来移动播放器,但是我不希望播放器离开屏幕。如何以一种体面的方式限制它?
void onPanUpdate(DragUpdateDetails details) {
if(game.screenSize.width >= playerRect.right && playerRect.left >= 0
&& game.screenSize.height >= playerRect.bottom && playerRect.top >= 0) {
playerRect = playerRect.translate(details.delta.dx, details.delta.dy);
} else {
playerRect = playerRect.translate(-details.delta.dx*4.2, -details.delta.dy*4.2);
}
}
我想出了这个解决方案,但是如您所知,它并不酷。我需要任何帮助。
答案 0 :(得分:1)
很高兴见到我们游戏引擎的另一个用户!检查中还必须考虑增量。这样的事情应该起作用:
void onPanUpdate(DragUpdateDetails details) {
final delta = details.delta;
final size = game.screenSize
double translateX = delta.dx;
double translateY = delta.dy;
// Make sure that the player never goes outside of the screen in the X-axis
if (playerRect.right + delta.dx >= size.width) {
translateX = size.width - playerRect.right;
} else if (playerRect.left + delta.dx <= 0) {
translateX = -playerRect.left;
}
// Make sure that the player never goes outside of the screen in the Y-axis
if (playerRect.bottom + delta.dy >= size.height) {
translateY = size.height - playerRect.bottom;
} else if (playerRect.top + delta.dy <= 0) {
translateY = -playerRect.top;
}
playerRect = playerRect.translate(translateX, translateY);
}
如果您有更深入的问题,我建议您也加入我们的不和谐之中: https://discord.gg/pxrBmy4