【问题标题】:stop a player when collides with tiles in android与android中的瓷砖碰撞时停止播放器
【发布时间】:2013-04-22 17:32:14
【问题描述】:

我正在尝试在 mario zechner 的框架 badlogicgames 上制作一个简单的游戏。我只是想放置一些瓷砖,比如说......树木,灌木等,并希望我的玩家在经过它们时停下来......以产生良好的效果......我尝试了很多替代方案......但没有奏效......

1.) 我试图定义一个布尔值playerBolcked = false; ...在碰撞检测循环中,我将其设置为 true .. 当它为 true 时 .. 我阻止了玩家移动 ..update();

2.) 我试图在检查碰撞之前存储玩家的位置..如果玩家与瓷砖碰撞... 然后我再次将位置重新设置...它也没有工作...

我的检测码是这样的……

private void checkTreeCollisions() {
        int len = trees.size();
        float x = allen.position.x;
        float y =allen.position.y;



     for (int i = 0; i < len; i++) {
                Tree tree = trees.get(i);

                    if (OverlapTester.overlapRectangles(allen.bounds, tree.bounds)) {

                            // this is not working
                        allen.position.set(x, y);


                        break;

                }
        }
    }

请给我一个好的方法...

【问题讨论】:

    标签: java libgdx collision-detection collision


    【解决方案1】:

    在您的碰撞检测中,首先检查它是否在 x 轴上水平碰撞,然后将玩家在 x 上的速度设置为零。然后垂直检查,如果玩家在 y 轴上发生碰撞,则将其 y 速度设为零。

    This 是一个好的开始。

    【讨论】:

      【解决方案2】:

      问题是,在您的代码中,您正在保存玩家的位置并同时检查碰撞。您需要做的是在保存玩家的位置之后并在检查碰撞之前移动玩家:

      private void checkTreeCollisions() {
          int len = trees.size();
          //Save your player position here
          float x = allen.position.x;
          float y =allen.position.y;
      
          //Move your player move code here, so he moves, then checks collitions
      
          for (int i = 0; i < len; i++) {
               Tree tree = trees.get(i);
      
               if (OverlapTester.overlapRectangles(allen.bounds, tree.bounds)) {
      
                   //If collision, then set the player to his/her previous position
                   allen.position.set(x, y);
                   break;
               }
          }
      }
      

      您可以使用相同的逻辑划分 x 轴和 y 轴的移动,并且您将能够在对象侧“滑动”,这看起来比停止时要好得多。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-26
        相关资源
        最近更新 更多