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

2015/3/20

Loading in Actors and If touching Actor

LamboCreeper LamboCreeper

2015/3/20

#
Hey! I am wondering how I do two things. 1. How do I detect if Actor A is touching Actor B? 2. How do I make it so when "Reset" is pressed Actor A and B show. Thanks.
EduandErnst EduandErnst

2015/3/20

#
1. There are several ways to detect another object. getOneIntersectingObject(java.lang.Class cls) getOneObjectAtOffset(int dx, int dy, java.lang.Class cls) You'd write it like...
Actor actor = getOneIntersectingObject(null); // place the class you want to check for, instead of null write "yourclass.class" without "".
if (actor != null){
place some code
} else {
place some more code
}
or have have a look at Greenfoot class documentation.. 2. Place the objects into your world --> right click with your mouse --> safe the world
Super_Hippo Super_Hippo

2015/3/20

#
1. in class A
if (isTouching(B.class))
{
    //do something
}
2. Either do it with the safe-the-world feature as described in the last post, or you do it manually. In the world constructor (so after the 'super' call):
addObject(new A(), 20, 10);
addObject(new B(), 30, 50);
Adjust the values to your needs.
danpost danpost

2015/3/20

#
There is also an Actor class method 'intersects(Actor)' which you can use if you have a reference to the actor you are checking the intersection of.
/// in ActorA class
private Actor actorB; // as instance field set to the actor to intersect with
// in act
if (intersects(actorB)) { ... }
You need to login to post a reply.