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

2013/12/14

Why does the ball remove itself from the world?

Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
My Scenario Why does the ball remove itself from the world? It's clearly not at the x and y coordinates of the box.
bourne bourne

2013/12/14

#
getObjectsAtOffset(0, 0, Box.class) != null is always true since getObjectsAtOffset returns a list, never null should check instead whether the list returned is not empty
Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
Then why does it work here?
    void pickUpBall()
    {
        if (Greenfoot.isKeyDown("p")) {
            if (getObjectsAtOffset(0, 0, Ball.class) != null) {
                isHoldingBall = true;
                ball.isBeingHeld = true;
            }
        }

        carryBall();
    }
And how would I check to see if the list is empty?
bourne bourne

2013/12/14

#
from your code above, !getObjectsAtOffset(0, 0, Ball.class).isEmpty()
danpost danpost

2013/12/14

#
'getObjectsAtOffset' always returns a List object. It may be an empty list, but it is still a list, and therefore it cannot be 'null'.
Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
bourne wrote...
from your code above, !getObjectsAtOffset(0, 0, Ball.class).isEmpty()
Okay, I understand this bit of code.
Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
danpost wrote...
'getObjectsAtOffset' always returns a List object. It may be an empty list, but it is still a list, and therefore it cannot be 'null'.
Then why does it work in this method below?
    void pickUpBall()  
    {  
        if (Greenfoot.isKeyDown("p")) {  
            if (getObjectsAtOffset(0, 0, Ball.class) != null) {  
                isHoldingBall = true;  
                ball.isBeingHeld = true;  
            }  
        }  
      
        carryBall();  
    }  
I've updated my scenario so that you can see that it does, in fact, work and allows the man to pick up his ball.
Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
& I've also commented out the erroneous code that causes the ball to remove itself upon running my scenario.
bourne bourne

2013/12/14

#
Why does carryBall get called outside of the nested if statements?
Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
Because it leads to this.
    void carryBall()
    {
        if (isHoldingBall) {
            ball.setLocation(getX(), getY());
        }
    }
The source code of my scenario is published, so you can look at it.
bourne bourne

2013/12/14

#
if (Greenfoot.isKeyDown("p")) {
            if (getObjectsAtOffset(0, 0, Ball.class) != null) {
                isHoldingBall = true;
                ball.isBeingHeld = true;
            }
        }
From your code above, the following always returns true: getObjectsAtOffset(0, 0, Ball.class) != null so the block of code is executed when Greenfoot.isKeyDown("p") returns true
bourne bourne

2013/12/14

#
That's why you can pick up the ball from across the World.
Lethal_Vitamin Lethal_Vitamin

2013/12/14

#
I never tried pressing the "p" key without being on the ball, huh. I fixed my original problem too. Thank you, bourne.
You need to login to post a reply.