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

2017/12/9

Incorrect Values

CavemanWrath CavemanWrath

2017/12/9

#
I cannot figure out what value I am supposed to use instead of null in this scenario. The code will compile perfectly fine, but the outcome I intended is that the amountTouching variable would only be added if there is an object in the position given.
public void checkProximity()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (getWorld().getObjectsAt(getX() + 51, getY(), Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld().getObjectsAt(getX() - 51, getY(), Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld().getObjectsAt(getX(), getY() + 51, Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld().getObjectsAt(getX(), getY() - 51, Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld().getObjectsAt(getX() + 51, getY() + 51, Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld(). getObjectsAt(getX() + 51, getY() - 51, Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld().getObjectsAt(getX() - 51, getY() + 51, Teacher.class) != null)
            {
                amountTouching++;
            }
            if (getWorld().getObjectsAt(getX() - 51, getY() - 51, Teacher.class) != null)
            {
                amountTouching++;
            }
        }
        if (amountTouching > 1)
            {
                setImage("JOHNSON.png");
            }
    }
danpost danpost

2017/12/9

#
The 'getObjectsAt' method always returns a List object (it never returns a 'null' value). You can ask what the size of the list is or if it is empty to determine if a Teacher object was detected.
Super_Hippo Super_Hippo

2017/12/9

#
Maybe this is working:
amountTouching += getNeighbours(51, true, Teacher.class).size();
CavemanWrath CavemanWrath

2017/12/10

#
Super_Hippo wrote...
Maybe this is working:
amountTouching += getNeighbours(51, true, Teacher.class).size();
That worked for the most part, but for some reason on some of the items, it takes multiple clicks for the program to run. I know that this is not a timing error because if I wait between clicks, it doesn't work any less randomly.
danpost danpost

2017/12/10

#
In your original code, try replacing 'getWorld().getObjectsAt' with 'getOneObjectAtOffset' and subtract 'getX' and 'getY' from the parameters. Although, it seems that the entire code can be replaced with this:
if (Greenfoot.mouseClicked(this))
{
    amountTouching += getNeighbours(51, true, Teacher.class).size();
    if (amountTouching > 1) setImage("JOHNSON.png");

}
Notice that line 3 is precisely the line Hippo provided.
danpost danpost

2017/12/10

#
Something that probably should be made clear: is it possible that a single neighboring Teacher object can encompass more than one of the locations you are checking? -- because that would change what we perceived to be what might fix the issue. Maybe the better question is what exactly are you intending the code you gave to do (be very detailed in what you actually want).
CavemanWrath CavemanWrath

2017/12/11

#
What you guys gave me worked perfectly for what I want. Thanks!
You need to login to post a reply.