Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java both if() and else if() statements are being called

Tags:

java

Okay, so I'm in the progress of making a game, and I need the collisions to work. I have an if() { } else if() {} -statement, but BOTH of them are being called. Here is my code:

Inside my Player Class:

public Rectangle[] tileRect;

    public void update() {
    tileRect = new Rectangle[Level1.tiles.size()];
    for (int w = 0; w < Level1.tiles.size(); w++) {
        Tile m = (Tile) Level1.tiles.get(w);
        tileRect[w] = m.getBounds();
        if(tileRect[w].intersects(getRect())) {
            System.out.println("intersecting");
        dy = 0;
        } else if (!tileRect[w].intersects(getRect())){
            dy = 4;
        }
    }
    x += dx;
    y += dy;

}

public Rectangle getRect() {

    return new Rectangle(x, y, 32, 32);
}

Here is my Tile class (the Level1.tiles is an arrayList of tiles):

package level;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Tile extends Rectangle {

// Defines the id of the tile, used for setting textures.

private static final long serialVersionUID = 1L;

public int id;

// Location

public int x;
public int y;

// Variables for the terrain sprite sheet.

public BufferedImage image;
public String imageLocation;
public BufferedImage[] sprite;
public int rows = 16;
public int collumns = 16;
public int width = 16;
public int height = 16;

public Tile(int idVal, int xPos, int yPos) {
    x = xPos * 32;
    y = yPos * 32;
    id = idVal;

    setBounds(x, y, 32, 32);

    createImages();
}

public BufferedImage getImage() {
    return sprite[id];
}

public void createImages() {

    imageLocation = "res/tile/terrain.png";
    try {
        image = ImageIO.read(new File(imageLocation));
    } catch (IOException e) {
        System.out.println("Unable to find file " + imageLocation
                + ", printing stack trace!");
        e.printStackTrace();
    }

    sprite = new BufferedImage[rows * collumns];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < collumns; j++) {
            sprite[(i * collumns) + j] = image.getSubimage(j * width, i
                    * height, width, height);
        }
    }
}

public int getXLoc() {
    return x;
}

public int getYLoc() {
    return y;
}


public void setX(int xPos) {
    x = xPos;
}

public void setY(int yPos) {
    y = yPos;
}

}

I'm getting the "intersecting" message in the console, but the player is still falling down (because dy = 4). Please help! I've been trying to solve this all morning...

like image 216
loafy. Avatar asked Jan 23 '26 14:01

loafy.


1 Answers

If and Else cannot both be caught at the same time.

Looks like you are looping through, seeing an intersection, then continuing to loop through regardless.

Try adding a break command to your for loop.

 if(tileRect[w].intersects(getRect())) {
     System.out.println("intersecting");
     dy = 0;
     break;
 } else if (!tileRect[w].intersects(getRect())){
     dy = 4;
 }

The break will stop your for loop from continuing and you will exit the loop with dy = 0; rather than going onto the next tile and changing it back to dy = 4;

like image 133
biddulph.r Avatar answered Jan 25 '26 03:01

biddulph.r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!