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

2016/4/27

Relationships between actors

Itta Itta

2016/4/27

#
This may be an obvious thing I'm missing, but I want to have one object perform an action only when it is to the right of another thing. The objects are "Hiro" and "Ykey", and I want Ykey to perform an action when it is to the right of Hiro. Help?
danpost danpost

2016/4/27

#
You could have Ykey move left, check for Hiro and move back, then perform any action needed if Hiro was there:
// in Ykey class (part of act)
setLocation(getX()-getImage().getWidth(), getY());
Actor hiro = getOneIntersectingObject(Hiro.class);
setLocation(getX()+getImage().getWidth(), getY());
if (hiro != null)
{
    // perform action here
}
You need to login to post a reply.