Skip to main content
1 of 3
LRFLEW
  • 103
  • 1

AttackingHobo had the right idea. you can write the same thing, though, as:

if(this.left>other.right || this.left<other.right)return false;
if(this.bottom>other.top || this.bottom<other.top)return false;

(note the change from other to this)

However, this might cause a lack of collisions. This is saying if the position of the bullet image is EXACTALLY the same as the enemy, it will hit. You probobly want some range (based on the size of the enemy and bullet). Try something like this:

final int distance = 10; //set to the number of pixel distance you want the max to be for the collision to occur
if (Math.abs(this.left-other.left) > distance) return false;
if (Math.abs(this.right-other.right) > distance) return false;
if (Math.abs(this.up-other.up) > distance) return false;
if (Math.abs(this.down-other.down) > distance) return false;

return true;

Try that and see how it works. You may need to play with how big distance is, so I separated it from the rest of the code. I hope this helps.

LRFLEW
  • 103
  • 1