The player and the enemy don’t collide.
They are layered together on different panels
layeredPane.add(c, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(s, JLayeredPane.PALETTE_LAYER);
In the circle file, I have this:
public boolean collidesWith(Sprite sprite) {
if(sprite.getX() > x && sprite.getX() < (x + 50))
{
if(sprite.getY() > y && sprite.getY() < (y + 50))
{
return true;
}
}
return false;
}
Where supposedly it is checked every time a key is pressed:
for(Sprite s: sprites) {
System.out.println("Checking collision: Circle at (" + x + ", " + y + "), Sprite at (" +
s.getX() + ", " + s.getY() + ")");
if(collidesWith(s)) {
System.out.println("Collision detected!");
setXY(20, 120);
}
}
I tried to put both the Player sprite and the enemy on the same file, but that doesn’t work.
This didn’t work either:
public boolean checkCollision(Sprite sprite) {
Rectangle circleBounds = new Rectangle(x, y, 50, 50);
Rectangle spriteBounds = new Rectangle(sprite.getX(), sprite.getY(), sprite.getWidth(),
sprite.getHeight());
return circleBounds.intersects(spriteBounds);
}
}
New contributor
Skitty12 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.