Hey guys I am a sophpmore in high school and just started computer prgramming this year, right now I am making my first game in greenfoot, which will be a kind of Plants vs Zombies game just with kids as plans and parents as zombies. I am trying to figure out how the kid can tell when the parent is inside of the screen and if it is start shooting the Ball. This is what i have for the getObjectsInRange method. Though non of my attempt on different codes have worked.
public boolean isParentInRange()
{
if (!getObjectsInRange(1200, Parent.class).isEmpty()){
return Parent != null;
}
then I dont know how to get further, just if it helps this is my ball code:
public void act()
{
setLocation(getX() + speed, getY());
checkBoundaries();
destroyEnemies();
}
public void checkBoundaries()
{
if(getX() > getWorld().getWidth() - 1)
getWorld().removeObject(this);
else if (getX() < 1)
getWorld().removeObject(this);
if (getY() > getWorld().getHeight() - 1)
getWorld().removeObject(this);
else if (getY() < 1)
getWorld().removeObject(this);
}
public void destroyEnemies()
{
Actor Parent = getOneIntersectingObject(Parent.class);
if(Parent != null) {
getWorld().removeObject(Parent);
getWorld().removeObject(this);
}
}
private int speed = 5;
}