无法在游戏中使用嵌套循环

eaf3rand  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(308)

我正在做一个游戏,我需要和我的球员关上一扇门。当他接近时,他有一个接近的动画。在我当前的循环中,检查每个房间的每个门。只有当有一扇门且该扇门位于环路中的第一个房间时,该代码才起作用。如果不是,则关闭动画仅对一帧触发,然后返回空闲状态,因此在关闭时在两帧之间闪烁。

for (Room r : rooms) {
   for  (Door d : Doors) {
      if (r.closed(d)) {
         close();
         d.closing=false;
         break;
      } else {
         d.closing=true;
         r.closed=false;
         r.locked=false;
      }
   }
}
flmtquvp

flmtquvp1#

代码中的这两行很可能导致问题:

player.isPushingRight=false;
    player.isPushingLeft=false;

当有物体开始下落时,它们就会被执行。通常情况下,这个秋天是由玩家发起的,所以在这里可能会有一些意义,尽管可能有一个更合适的地方放这些线。
真正的问题是你如何判断一个物体是否开始坠落:你检查每个平台,一旦你找到一个物体没有落在上面的平台,你就让它开始坠落。如果稍后找到对象所在的平台,则取消坠落,但播放器动画已停止。

void objGround(ArrayList<Platform> platforms, ArrayList<Pushable> pushObj) {// to see if the object is standing on a platform.

for (Pushable pu : pushObj) {
    pu.render();
    let isOnGround = false;
    for  (Platform plt : platforms) {
      if (pu.objectOnGround(plt)) {
        pu.moveY=0;
        pu.falling=false; 
        if (pu.y>pu.startingY+20) {
          pu.smashed=true;
        }
        isOnGround = true;
        break;
      }
    }

    if (!isOnGround && !pu.falling) {
      pu.falling=true;
      pu.moveY=2;
      pu.startingY=pu.y; // Guess you need that too
      player.isPushingRight=false;  // only valid if the player is the only cause of a falling object!
      player.isPushingLeft=false;  // only valid if the player is the only cause of a falling object!
    }
  }
} // end of objground procedure

相关问题