1

我试图通过拖动来移动我的播放器,但我不希望我的播放器离开屏幕。我怎样才能以体面的方式限制它?

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);
  }
}

我想出了这个解决方案,但你知道它并不酷。我感谢任何帮助。

4

1 回答 1

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);
}

如果您有更深入的问题,我建议您也加入我们的 discord: https ://discord.gg/pxrBmy4

于 2020-09-22T12:49:55.897 回答