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

2019/10/28

How can I move an Actor to another Actor?

Starlord_20 Starlord_20

2019/10/28

#
hi, I have a problem, I want an actor to detect when there is a certain other actor in the world and then automatically move to it. Is that possible somehow?
danpost danpost

2019/10/28

#
Starlord_20 wrote...
hi, I have a problem, I want an actor to detect when there is a certain other actor in the world and then automatically move to it. Is that possible somehow?
You can get a list of all actors of any type with:
1
java.util.List actors = getWorld().getObjects(Actor.class);
Replace Actor with any Actor subclass name for a specific type of actor. If the list, actors, is not empty, you can get a reference to the actor listed (presuming no more than one is ever in the world). With that reference, you can get the coordinates to turn toward (or move toward):
1
2
3
4
5
6
7
if (! actors.isEmpty())
{
    Actor actor = (Actor)actors.get(0);
    turnTowards(actor.getX(), actor.getY());
    move(3);
    // setRotation(0); // to maintain upright actor
}
Starlord_20 Starlord_20

2019/10/28

#
Works, thank you very much! :)
You need to login to post a reply.