Problems using collisions

For some collision aren’t working in my game. I’ve tried other ways and still could not get it to work. The player is suppose to stay on the floor and when a peice of the floor is gone the player can fall through it.

Main
http://pastebin.java-gaming.org/0c47d43634619
Game Screen
http://pastebin.java-gaming.org/f0c473d634316
Images


If you mean the player isn’t falling correctly, it may have something to do with this:

public void update() {
for(Rectangle rect: floor) {
if(collidesWith(rect))
pos.y = -270;
}
this.texture.setPosition(pos.x, pos.y);
}

It seems like you need an else statement to say
else pos.y += fallingSpeed

  public boolean collidesWith(Rectangle tile) {
     if (this.texture.getX() < tile.x + tile.width && this.texture.getX() + this.texture.getWidth() > tile.x 
           && this.texture.getY() < tile.y + tile.height && this.texture.getHeight() + this.texture.getY() > tile.y)
              return true;
     else
        return false;
  }

If I am reading it correctly the Player collidesWith method does not seem to check for partial collisions of the two rectangles, it seems to be a is isFullyContainedIn method instead.

I’m sorry I am new to this. How would I go about fixing this?

Do you have a force like gravity that is pulling on the player? Because from what I can tell, how you have it now is that if the player is colliding with the floor, you set his y position. But what happens if he is not colliding with the floor? Unless you have gravity his y position will stay where it is so he wont move.

If he isn’t colliding with the floor then he should fall down. So I have to add gravity to the player?

Yes, you could do something like this:
Have 2 variables for the player, y position, and gravity. Set gravity equal to a positive number, like 2. In the player’s update method, player y position = player y position + gravity. Then you will have to check if the player is on the floor. If the player’s y position + the player’s height is > the floor’s y position, set gravity = 0, else, gravity = 2.

You can also make the gravity variable more realistic by increasing it as the player is falling, but I would just do the easy way for now and understand what it’s doing.

Okay thank you a lot