So in my program I want to have two wolves that chase my sheep. The problem is I get an error that stops my program from running.
Both of the wolves have this code. I know it is because when one wolf eats the sheep the other has no idea what to do. Is there anyway to fix it?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Wolf here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Wolf extends Actor
{
public static int slowTimer;
/**
* Act - do whatever the Wolf wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int dist = 1000;
Actor closest = null;
Actor Sean = (Actor)getWorld().getObjects(Sean.class).get(0); // The error occurs on this line when one Wolf gets Sean (the Sheep)
if(!getObjectsInRange(dist, Sean.class).isEmpty())
{
for (Object obj: getObjectsInRange(dist, Sean.class))
{
int seanDist = (int) Math.hypot(Sean.getX() - getX(), Sean.getY() - getY());
if (closest == null || seanDist< dist)
{
closest = Sean;
dist = seanDist;
}
}
turnTowards(closest.getX(),closest.getY());
}
if (!getIntersectingObjects(Rock.class).isEmpty())
{
move(-4);
}
if(getOneIntersectingObject(Sean.class) != null)
{
Actor Wolf = getOneIntersectingObject(Sean.class);
if( Sean == Wolf )
{
getWorld().removeObject(Sean);
}
}
if (slowTimer > 0)
{
move(2);
}
else
{
move(3);
}
/** in World class act method (or a method it calls) */
if (Wolf.slowTimer > 0)
{
Wolf.slowTimer--;
}
}
}
