Extending upon the previous post, while refactoring the Tower class in the Tower Defence I made a mistake that resulted in a rather cool looking bug.

I’d switched over from using the primative types int x; int y to using Point pos to represent internally the position of each Tower.

Now when a monster is detected within the range of the most basic tower, if the gun has reloaded it fires a new bullet using the following code:

bulletArray.add(new JavaTowerDefenceBullet(pos,myMonster.position(),myMonster));

And within the bullet code, we have the move method as follows

        lifeSpan--;
 
        int myMove = Math.min(speed, (int) pos.distance(destination));
 
        double distance = pos.distance(destination);
 
        double dxang = Math.acos( (destination.x - pos.x) / distance);
        double dyang = Math.asin( (destination.y - pos.y) / distance);
 
        int dx = (int) (Math.cos(dxang) * myMove);
        int dy = (int) (Math.sin(dyang) * myMove);
 
        pos.translate(dx, dy);

Unfortunately I’d forgotten that I was passing via reference rather than value!

Thus rather than the tower fireing a bullet which moved towers the monster, it was firing ITSELF! And there were flying towers all over the map :P

A quick change to the Tower code to

bulletArray.add(new JavaTowerDefenceBullet((Point)pos.clone(),myMonster.position(),myMonster));

and it all works as expected. Though in some ways I prefered firing entire towers at the monsters, simply because they’re bigger.