比较和创建Point 2D数组

时间:2014-02-24 03:09:26

标签: java arrays for-loop double

所以我仍然试图让它工作......我需要它告诉我用户位置匹配数组中其中一个墙的位置。

我将所有坐标输出到控制台中,一切看起来都正确,但是当它们相等时它不起作用。 (这在以前有用......似乎如果我不在循环中创建墙壁它会起作用但是一旦我宣布太多墙壁它似乎退出工作(不完全确定但我设置地图没有循环和它没有一贯地工作,我无法弄清楚原因..

GameObject(user).positionPoint2D并且`setPosition设置了位置,

public class Map {
    public ArrayList<Wall> theseWalls = new ArrayList<Wall>();
    private boolean isSomethingThere;

    public Boolean somethingIsThere(GameObject user){
        for(int i = 0; i < theseWalls.size(); i++){
            GameObject a = (Wall) theseWalls.get(i); 

            if( user.returnPosition().equals(a.returnPosition())){
                isSomethingThere= true;
            } else {
                isSomethingThere= false;
            }
        }
        return isSomethingThere;
    }
}

以便检查它们是否撞墙

这就是墙壁

public void makeWallWE(double starty, double endy, double x ){
    double length = endy - starty;
    for(int i = 1; i<=(int)length;i++){

        Wall Wallie= new Wall();
        Wallie.setPosition(x,starty+i);

        Wallie.showPosition();

        theseWalls.add(Wallie);
    }
}

1 个答案:

答案 0 :(得分:0)

你有:

public Boolean somethingIsThere(GameObject user){
    for(int i = 0; i < theseWalls.size(); i++){
        GameObject a = (Wall) theseWalls.get(i); 

        if( user.returnPosition().equals(a.returnPosition())){
            isSomethingThere= true;
        } else {
            isSomethingThere= false;
        }
    }
    return isSomethingThere;
}

即使用户的位置与其中一个墙匹配,您仍会继续检查其余墙壁,最后将isSomethingThere设置为false。唯一有效的时间是theseWalls中的最后一个墙是匹配的墙。

当您找到匹配的墙时,您会希望突破该循环,以避免在列表中稍后检查不匹配的墙时将isSomethingThere重置为false