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

2021/8/6

Make an object move towards another object

xixEmilyxix xixEmilyxix

2021/8/6

#
I had 2 different objects which are two different types of bees. I was wondering if i could make it so that one type of bee would target an object flower and the other would only move inside a certain place (which will be the hive), Not sure how to go about this. I have code to make the bees move around normally which is in the parent class. This is the code i have:
public class Bee extends Actor
{
    public void flyAround()
    {
        //go forward
        move(2);
        //turn randomly
        if(Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(30) - 15);
        }
        //if hit side, turn
        if(getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
           turn(100); 
        }
        //if hit side, turn
        if(getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(100);
        }
    }
}
danpost danpost

2021/8/7

#
xixEmilyxix wrote...
Imake it so that one type of bee would target an object flower and the other would only move inside a certain place (which will be the hive), I have code ... in the parent class.
Have both child classes call the flyAround method from their respective act methods, but have the one that is to stay in the hive modified to restrict where it can go and the other modified with an if-else to seek out flowers. A reference field for a flower actor might be useful in this regard (set it to flower when "seen" and use "if null" as condition in aforementioned if-else).
You need to login to post a reply.