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

2016/2/7

getOneIntersectingObject(<Class>);

TomazVDSN TomazVDSN

2016/2/7

#
Hey guys, here the novice again asking basic questions. If we have two blocks, B1 and B2. What the correct way to use getOneIntersectingObject(<Class>)? Does getOneIntersectingObject(B1.class) returns the Actor b1? Should I consider the actor the one that is moving ? Regards, Tomaz
danpost danpost

2016/2/7

#
TomazVDSN wrote...
If we have two blocks, B1 and B2. What the correct way to use getOneIntersectingObject(<Class>)?
If you have two separate classes, a B1 and a B2 class, that the b1 and b2 objects are created from, then the method will return the object of whichever class you supply. If both objects are created from the same class, then further testing will be needed to determine which one would be returned, if any.
TomazVDSN TomazVDSN

2016/2/7

#
Gentlemen, I read in the documentation: getOneIntersectingObject(java.lang.Class<?> cls), Return an object that intersects this object. So if B1 "bump" into B2 and I wrote getOneIntersectingObject(B1.class) it should return B2. and if B1 "bump" into B2 and I wrote getOneIntersectingObject(B2.class) it should return B1. I will do some more testing. Regards, Tomaz
danpost danpost

2016/2/7

#
TomazVDSN wrote...
So if B1 "bump" into B2 and I wrote getOneIntersectingObject(B1.class) it should return B2. and if B1 "bump" into B2 and I wrote getOneIntersectingObject(B2.class) it should return B1.
What you say here is not what the documentation says. The type of object returned will be of the type given as the argument to the method. If you wrote getOneIntersectingObject(B1.class) it will return an object of the B1 class (or null). If you wrote getOneIntersectingObject(B2.class) it will return an object of the B2 class (or null).
TomazVDSN TomazVDSN

2016/2/7

#
danpost, thanks I was totally my mistake.. afterwards I did a test just to see it working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*;
 
public class OrangeBlock extends MovementController
{
    public void act() {
        checkBumpWith(GreenBlock.class);
    }
    public void checkBumpWith(Class myClass){
         
        Actor actor = getOneIntersectingObject(myClass);
        if(actor!=null){
            actor.setLocation(20, 20);
        }
    }
}
You need to login to post a reply.