Java同时调用if()和else if()语句

时间:2014-02-28 15:02:24

标签: java

好的,所以我正在制作游戏,我需要碰撞才能工作。我有一个if() { } else if() {} - 语句,但它们中的两个都被调用了。这是我的代码:

在我的玩家类中:

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

这是我的Tile类(Level1.tiles是tile的数组列表):

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

}

我在控制台中收到“交叉”消息,但玩家仍然在摔倒(因为dy = 4)。请帮忙!我整个上午一直试图解决这个问题......

2 个答案:

答案 0 :(得分:1)

如果和其他人不能同时被捕获。

看起来你正在循环,看到一个交叉点,然后继续循环,无论如何。

尝试在for循环中添加break命令。

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

中断将阻止您的for循环继续,您将使用dy = 0;退出循环,而不是转到下一个磁贴并将其更改回dy = 4;

答案 1 :(得分:0)

无法在同一次运行中调用

ifelse if。如果if条件为真,那么任何else都不会运行(甚至不会检查)。这可能是不同的运行。调试或放置日志以查看流程。

另外

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

写得更简单

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