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

2013/1/6

How is the world divided in cells? Problems with getObjectsInRange()

JimmyFake JimmyFake

2013/1/6

#
I'm having some difficulties with using the getObjectsInRange-method from the Actor.class. I read the description in the GreenfootDocumentation and it says the radius is measured in cells. No matter what I type in for the radius and no matter how far my Hero is away from the Box ( which checks on the Range of the Hero) the the method always return that the Hero is in Range. Here's my code:
public class Box extends Obstacle
{
    public void turnRed()
    {
        getImage().setColor(new Color(255,0,0));
        getImage().fill();
    }
    public void turnBlue()
    {
        getImage().setColor(new Color(0,0,255));
        getImage().fill();
    }
    
    public void act() 
    {
        if(getObjectsInRange(1,Hero.class) != null) turnRed();
        else turnBlue();
    }  
I guess my problem is that I don't quite understand how a world is build. A world is build with the following super(int worldWidth, int worldHeight, int cellSize). My level1 world is has the following size: super(1100,550,1). That would mean a world with 1100x550 cells with a cell size of 1x1 pixels. This would mean, that the box shouldn't even turn red, because the center of Hero and the center of Box can't even come as close, as they need to trigger the if-clause true, because there is some code in Hero that prevents collision of those two. Did I misunderstand something? What am I doing wrong? I already tried making the world with bigger cells, but still the same results. Is there a preference in greenfoot that can make the cells visible via a grid? Thank you
danpost danpost

2013/1/6

#
If your Box object is rectangular, then why are you using the 'getObjectsInRange' method? which checks for a circular area around the centerpoint of the image of the box. Maybe I am mistaken as to what you need; so, please give a detailed explanation of how you want the Box and Hero objects to interact.
Gevater_Tod4711 Gevater_Tod4711

2013/1/6

#
The problem is that the method getObjectInRange() will never return null. If there is a hero in the range (one in this case) it'll return a list with this hero in it. If there is no hero the method will return a empty list which is quite different to a nulltype. If you change your code in the act method like this:
public void act() {  
    if(!getObjectsInRange(1,Hero.class).isEmpty()) {
        turnRed();
    }
    else {
        turnBlue();
    }
}
It should work.
danpost danpost

2013/1/6

#
If you collision detection which negates any intersecting of the Hero and Box objects, maybe you can do something like the following in the Box class act:
public void act()
{
    GreenfootImage image = getImage();
    setImage(new GreenfootImage(image.getWidth()+1, image.getHeight()+1));
    boolean goRed = false;
    if (!getIntersectingObjects(Hero.class).isEmpty()) goRed = true;
    setImage(image);
    if (goRed) turnRed(); else turnBlue();
}
You may have to increase the amount of increase in size of the image ("+1" to "+2") to make it work right.
JimmyFake JimmyFake

2013/1/6

#
Uhm I just want to Box to react(turn Red) if the Hero to near to it. The radius shouldn't be just 1. I'm thinking about an radius that about 3-4 the box's length. The Box is just exemplary. For my game I could use this for example like following: I have an Hero(my Player), and an NPC standing somewhere. When the Hero steps into a defined range of the NPC, the NPC says something, like : "Oh hi, you're new here, aren't you!?"....
danpost danpost

2013/1/6

#
Then you could use something like I showed above; maybe change line 4 to something like:
setImage(new GreenfootImage(image.getWidth()+8, 1));
With this you are giving a 4-pixel range outsize the original image on either side and the Hero object would have to be somewhat at the same level height-wise.
JimmyFake JimmyFake

2013/1/6

#
Oh, so the mistake was, that there is always a list returned. Thank you Gevater_Tod that clears my confusion. As for your answer danpost, I did not understand it, but I don't have any time right now, to read it again. I'll look into it tommorow. But I think you're trying to tell me how I can see if the Hero's in range with an rectangular radius. a Thank you, both. :)
JimmyFake JimmyFake

2013/1/7

#
I had some time, to try out what you posted danpost. My assumption was right, even though I may have expressed myself, incorrectly. For now the ObjectsInRange method works better for me, because an circular radius is fine for now, and the code is simpler. And thanks to GevaterTod, I do now know, how to use that command :) But if I want to adapt the area of the radius, because of some reasons that might appear in future, your advice would come in handy, danpost. Thank you both, again :D
You need to login to post a reply.