In my balloon game, I am having trouble with the range aspect. if the tower sees a balloon within 200 units of it, it will turn towards it and start shooting. However, my problem is that as the balloons outpace the monkeys ability to shoot(which is what I want), the tower doesn't retarget on to a closer balloon, it keeps targeting the next closest balloon to the one already popped, even if the balloon is way out of range (obviously this isn't what I want). I want it to somehow see that this specific balloon is out of range so target one that is in range.
I can provide the code if needed.
Below is the tower code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class DartMonkey here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class DartMonkey extends Actor
{
/**
* Act - do whatever the DartMonkey wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public boolean inRange;
public boolean isShoot;
private int timer = 60;
public void act()
{
lookAt();
shoot();
}
public void shoot()
{
if (timer > 0 && inRange)
{
timer --;
if (timer == 0 && inRange)
{
getWorld().addObject(new Bullet(), getX(), getY());
timer = 60;
}
else if (getWorld().getObjects(RedBloon.class).size() == 0)
{
int x = 0;
}
}
}
public void lookAt()
{
if(!getObjectsInRange(200, RedBloon.class).isEmpty())
{
Actor b = getWorld().getObjects(RedBloon.class).get(0);
turnTowards(b.getX(), b.getY());
setRotation(getRotation()+90);
inRange = true;
}
else {
inRange = false;
}
}
}

