mcfly24 :
I'm trying to check collision for each cube in arraylist, but the result is, that collision is working only for last cube in arraylist..
public class Cube {
public int x, y;
private boolean conflict = false;
public Cube(int x, int y) {
this.x = x;
this.y = y;
}
public void moveDown() {
if(!conflict("down")) {
this.y += 18;
}
}
public boolean conflict(String dir) {
if(dir.equals("down")) {
for(Cube cubes : Panel.cubes) {
if(this.hashCode() != cubes.hashCode()) {
if(this.y + 18 == cubes.y && this.x == cubes.x || this.y >= Main.height - 18*4) {
this.conflict = true;
} else this.conflict = false;
}
}
}
}
}
desmaxi :
First of all your conflict method doesn't return
anything, I'm wondering how this compiles. But the problem is you never go out the for loop
when you find a collision.
public boolean conflict(String dir) {
if (dir.equals("down")) {
for(Cube cubes : Panel.cubes) {
if(this.hashCode() != cubes.hashCode()) {
if(this.y + 18 == cubes.y && this.x == cubes.x || this.y >= Main.height - 18*4) {
this.conflict = true;
break;
} else {
this.conflict = false;
}
}
}
}
return this.conflict;
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=328371&siteId=1