使用intersect或contains方法碰撞不起作用

时间:2013-09-10 03:27:19

标签: java collision contains intersect

我很抱歉,但是有一个问题,它似乎永远不会成为现实(当我可以清楚地看到它发生在我自己的眼睛!)。我以前做过很多碰撞检测方法,但由于某种原因,这个非常简短的方法给我带来了麻烦。感谢您提前花时间和任何帮助。

     ArrayList<UserInterface> ui = new ArrayList<UserInterface>();

blocks = new Image("res/DirtBlock.png");

    int uiWidth = 0;
    for(int y = 0; y <= blocks.getHeight(); y += 40){

        for(int x = 0; x < blocks.getWidth(); x += 40){
            a = new UserInterface(64 + uiWidth, gc.getHeight() - 64, 1);
            a.setSubX(x);
            a.setSubY(y);
            uiWidth += 40; //spacing between blocks on UI bar
            ui.add(a);
        }

    }

            Input input = gc.getInput();
        if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
            mouseRect = new Rectangle(input.getMouseX(), input.getMouseY(), 4, 4);
            UiCollision play = new UiCollision();
            play.uiDetection();
        }

public class UiCollision extends Editor{

public void uiDetection(){
    for (int n = ui.size() - 1; n >= 0; n--){
        UserInterface u = ui.get(n);

        if(u.getID() != 0) // Don't want to refresh the arrows which are id 0
            u.refresh();

        if (u.getRectangle().intersects(mouseRect) || u.getRectangle().contains(mouseRect)){
            blockID = u.getID();
            System.out.println("asdashdahs");
        }

    }
}

}

最后是ui类:

import org.newdawn.slick.geom.Rectangle;

public class UserInterface extends Editor{

Rectangle rect = null;
int id, x, y, subX, subY;


public UserInterface(int x, int y, int id){
    this.x = x;
    this.y = y;
    if (id == 0)
        rect = new Rectangle(x, y, 64, 128); // ui used for scrolling left & right
    else
        rect = new Rectangle(x, y, ts, ts);
    this.id = id;
}

public int getID(){
    return id;
}

public void refresh(){
    rect = new Rectangle(x + sideScroll, y, ts, ts); // Ui with an id of 0 shouldn't be effected by this.
}

public void setSubX(int a){
    subX = a;
}

public void setSubY(int a){
    subY = a;
}

public int getSubX(){
    return subX;
}

public int getSubY(){
    return subY;
}

public Rectangle getRectangle(){
    return rect;
}
}

0 个答案:

没有答案