Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

java - can't get my nested loops to work in my game

I am making a game and I need to close a door with my player. While he is closing he has a close animation. In my current loop which is checking each door on each room. The code works only if there is one door and that door is on the first room in the loop. If it is not then the close animation only fires for a frame then it goes back to idle, so while closing it blinks between the two.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Those two lines in your code most likely cause the problem:

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

They are executed when there is an object that starts falling. Usually, this fall would be initiated by the player, so it might make some sense here, though there might be a more appropriate place for those lines.

The real problem is how you determine if an object starts falling: you check every platform, and as soon as you find one where the object is not rested on, you let the object start falling. If you later find a platform where the object is rested on, you cancel the fall but the player animation is already stopped.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...