Flutter/Flame 限制游戏中的拖动区域
Flutter/Flame limiting the drag area in a game
我想通过拖动来移动我的播放器,但我不想让我的播放器离开屏幕。我怎样才能以一种体面的方式限制它?
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);
}
}
我想出了这个解决方案,但如您所知,它并不酷。我感谢任何帮助。
很高兴看到我们游戏引擎的另一个用户!检查中还必须考虑增量。这样的事情应该有效:
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);
}
如果您有更多 in-depth 问题,我建议您也加入我们的 Discord:
https://discord.gg/pxrBmy4
我想通过拖动来移动我的播放器,但我不想让我的播放器离开屏幕。我怎样才能以一种体面的方式限制它?
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);
}
}
我想出了这个解决方案,但如您所知,它并不酷。我感谢任何帮助。
很高兴看到我们游戏引擎的另一个用户!检查中还必须考虑增量。这样的事情应该有效:
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);
}
如果您有更多 in-depth 问题,我建议您也加入我们的 Discord: https://discord.gg/pxrBmy4