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

2017/1/15

Touching of two objects.

PenguinPower PenguinPower

2017/1/15

#
I need a little help. I have three actors: Boy, Pingu and Counter. I want to see if Boy and Pingu are touching in the third actor class (Counter). Can you help me? I'm searching this for 2 days. Can you help me guys? :)
Super_Hippo Super_Hippo

2017/1/15

#
If you place the code in the Boy class:
if (isTouching(Pingu.class))
{
    Counter c = (Counter) getWorld().getObjects(Counter.class).get(0);
    if (c != null)
    {
        //now you can call a method on c to do something with the counter
    }
}
If you want to place the code in the counter class:
Boy b = (Boy) getWorld().getObjects(Boy.class).get(0);
if (b != null && b.touchingPingu())
{
    //do something
}
with this in the Boy class
public boolean touchingPingu()
{
    return isTouching(Pingu.class);
}
PenguinPower PenguinPower

2017/1/16

#
Thank you so much :)
You need to login to post a reply.