This site requires JavaScript, please enable it in your browser!
Greenfoot back
BoZZWombAT
BoZZWombAT wrote ...

2016/11/24

how find the wombat the leafes and move to them ?

BoZZWombAT BoZZWombAT

2016/11/24

#
how find the wombat the leafes and move to them ?
Super_Hippo Super_Hippo

2016/11/24

#
There are several methods in the Actor class which you can use (see here). If there is only one leaf, it could look like this:
private Actor leaf;

public void act()
{
    if (leaf != null)
    {
        if (getX() < leaf.getX()) setLocation(getX()+1, getY());
        else (getX() > leaf.getX()) setLocation(getX()-1, getY());
        else (getY() < leaf.getY()) setLocation(getX(), getY()+1);
        else (getY() > leaf.getY()) setLocation(getX(), getY()-1);
        else removeTouching(Leaf.class);
    }
    else
    {
        leaf = getWorld().getObjects(Leaf.class).get(0);
    }
}
If there are more leaves in the world, then you could try to find nearest one (instead of the last line of code).
You need to login to post a reply.