Java 碰撞不是';t使用intersect或contains方法进行工作
我很抱歉,但有一个问题,它似乎从来都不是真的(当我可以清楚地看到它发生在我自己的眼睛!)。我以前做过很多碰撞检测方法,但由于某种原因,这个非常简短的方法给我带来了麻烦。谢谢你的时间和任何帮助Java 碰撞不是';t使用intersect或contains方法进行工作,java,collision,contains,intersect,Java,Collision,Contains,Intersect,我很抱歉,但有一个问题,它似乎从来都不是真的(当我可以清楚地看到它发生在我自己的眼睛!)。我以前做过很多碰撞检测方法,但由于某种原因,这个非常简短的方法给我带来了麻烦。谢谢你的时间和任何帮助 ArrayList<UserInterface> ui = new ArrayList<UserInterface>(); blocks = new Image("res/DirtBlock.png"); int uiWidth = 0; for(int
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();
}
最后是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;
}
}
在方法
ui检测
中,什么是ui
和'mousestright?因为它们肯定不是第一个代码片段中的局部变量
ui`和mous勃立
(除非您没有向我们展示一些重要的代码片段)。它们位于代码的第一部分。在编辑器类中,uiDetection类扩展了编辑器类。不过,在上面显示的代码中进行更新之前,我确实将mous勃起声明为null。Ie在第一个片段中对它们进行了全局更新。ui
在哪里填充?如果它是空的,uiDetection()
将不起任何作用。为什么每次都要创建一个新的UICollision实例,现在它如何处理UI的状态?对不起,你是对的,我忘了将该部分添加到描述中。阅读了我从UserInterface类中获取的一些内容,以及它们与生成相关的内容。我非常肯定,当我填充这些形状时,它们会发生碰撞,并且可以看到它的发生。
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;
}
}