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

2014/4/9

Linking one actor to other

Rishav_Agarwal Rishav_Agarwal

2014/4/9

#
I want that when an actor touches an moving actor, it also moves with the moving actor. How to do it?
Rishav_Agarwal Rishav_Agarwal

2014/4/9

#
Someone plz help me!!
danpost danpost

2014/4/9

#
// in class of followed actor
// instance field
private Actor follower;
// in act (or in a method it calls)
if (follower == null) follower = getOneIntersectingObject(FollowerClassName.class);
// when moving actor (using setLocation(getX()+dx, getY()+dy); where dx and dy are the changes in location
if (follower != null)
{
    if (follower.getWorld() == null) follower = null; else follower.setLocation(follower.getX()+dx, follower.getY()+dy);
}
You may not need the check to see if follower is still in the world. I put it there as a precautionary measure (just in case it was possible for it to be removed from the world). If the followed actor (actor of this class) is removed, you may want to also remove its follower (if not 'null'). Good programming takes consideration of all possibilities.
Rishav_Agarwal Rishav_Agarwal

2014/4/9

#
Thanks Danpost!!
You need to login to post a reply.