Hey there, so im new to greenfoot. im studying programming at my local college and i have an assignment due for sunday in which i need to make a 2d top down zombie game.
i have the game working however i need to see if one object is touching another in order to trigger a graphic.
The idea is that the zombie will drop an power up and when the survivor touches it, he gains a power. that all works. the problem lies in making the graphic work. I cant have the graphic as a subclass of the player as when i do this, the zombies chase the graphic.
Is there some code to check if one actor is touching another and if not, how can i edit this zombie code to only chase the survivor class and not the subclasses?
Thanks in advance :)
public class Zombie extends Actor
{
/**
* Act - do whatever the Zombie wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
followHuman();
move(1);
Actor Survivor;
Survivor = getOneObjectAtOffset(0,0, Survivor.class);
if (Survivor != null)
{
World world;
world = getWorld();
world.removeObject(Survivor);
Greenfoot.stop();
return;
}
}
public void followHuman()
{
double dist = 800;
Actor closest = null;
if(!getObjectsInRange(800, Survivor.class).isEmpty())
{
for (Object obj: getObjectsInRange(800, Survivor.class))
{
Actor guy = (Actor) obj;
double guyDist = Math.hypot(guy.getX() - getX(), guy.getY() - getY());
if (closest == null || guyDist< dist)
{
closest = guy;
dist = guyDist;
}
}
turnTowards(closest.getX(),closest.getY());
}
}
}

