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

2012/5/29

Help with "getOneIntersectingObjectAtOffset"

DevAvmeister DevAvmeister

2012/5/29

#
Hello! I'm trying to create an "if" statement that uses multiple "getOneIntersectingObjectAtOffset" booleans. It looks like this:
        if (canSee(PercyLSpencer.class)&& canSee(Raytheon.class) && canSee(ChocolateBar.class))
        {
            getWorld().addObject(new Microwave(),350,350);
        }
(the "canSee" method contains this:)
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
This code compiles fine, but if all the objects listed in the above statement are touching the acting object, nothing happens. However, if I change the code to only ONE "canSee" method
        if (canSee(PercyLSpencer.class))
        {
            getWorld().addObject(new Microwave(),350,350);
        }
the acting object will register that "PercyLSpencer" is touching it. Any suggestions?
ttamasu ttamasu

2012/5/29

#
The problem is that for canSee you are using the getOneObjectAtOffset() method. When it sees any actor, it will select one the the of actors surronding the acting object and it might not be the one you want. you need to modify your code to use getObjectsAtOffset which returns a List of actors. and then you can use the contains() method to see if the list contains the actor.
davmac davmac

2012/5/30

#
The problem is that for canSee you are using the getOneObjectAtOffset() method. When it sees any actor, it will select one the the of actors surronding the acting object and it might not be the one you want.
But, it will only select an actor of the specified class - so, the original code should be fine. DevAvmeister: I think we need to see more of your code to help with this problem; perhaps if you can upload the scenario?
DevAvmeister DevAvmeister

2012/5/30

#
Of course, here it is: http://www.greenfoot.org/scenarios/5316
danpost danpost

2012/5/30

#
With getOneObjectAtOffset(0, 0, clss), all the items must be touching the CENTER of 'this' actor. First, try 'getOneIntersectingObject(clss)', and if that does not solve the issue, re-upload your scenario (but, this time include the scource code <check the checkbox in the 'share' window>).
DevAvmeister DevAvmeister

2012/5/31

#
'getOneIntersectingObject' requires two integers along with 'clss', so just putting (clss) doesn't work. I'll upload with the source code, I mistakenly thought I had before. Thanks for your assistance! It doesn't look like the server is currently responding, I'll re-upload later, when I have time.
davmac davmac

2012/5/31

#
No, 'getOneIntersectingObject' has only one parameter, the class.
danpost danpost

2012/5/31

#
@DevAvmeister, did you refer to the documentation before posting? This is what I see in the Actor class API:
Greenfoot API - Actor class wrote...
protected Actor getOneIntersectingObject(java.lang.Class cls) Return an object that intersects this object.
As you can see, the method only has the one parameter, the class (which can be 'null', if it can be any class). When you said 'just putting (clss) doesn't work', you may not realize that 'clss' is a variable name that holds a value; for example: Class clss = Enemy.class, or Class clss = Scoreboard.class. Supply the parameter in its proper form and it should work.
DevAvmeister DevAvmeister

2012/5/31

#
I'm sorry, I was looking at 'getOneObjectAtOffset', my mistake :P Yes, 'getOneIntersectingObject' does work. Thank you!
You need to login to post a reply.