Below is my code that detects collision between two objects, setting a boolean to true every time my player collides with the wall:
private boolean collide(){
for(int i = 0; i < handler.object.size(); i++){
if(this.getBounds().intersects(handler.object.get(i).getBounds()) && handler.object.get(i).getId() != ID.Player && handler.object.get(i).getId().equals(ID.Wall)){
System.out.println("COLLIDEEEEE");
return true;
}
}
return false;
}
My intent is to have the player stop moving upon collision. Right now my Code for the player Movement looks like this:
public void tick() {
if (Var.A == true && Var.ableToMove == true) {
Var.OffsetX += Var.speed;
}
if (Var.D == true && Var.ableToMove == true) {
Var.OffsetX -= Var.speed;
}
What i now want to do is, I want the player stop upon colliding the wall. I would really appreciate if anyone of you could help me.
collide()method seems to work fine. - Sergey