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

2013/1/16

Color stays same after hit, but I want to revert back to original.

kaciedy kaciedy

2013/1/16

#
I want the color to return back to the "image1", but it remains "image2". I feel like it is a simple answer. Could you please help direct towards what I am doing incorrectly?
     public void colorChange()
    {
        List<Body> bodies = getIntersectingObjects(Body.class);
        
        for(Body b : bodies)
        {
            if(bodies != null)
            {
                setImage(image2);
                play();
            }
            
            if(bodies == null)
            {
                setImage(image1);
            }
        }
        
    }
danpost danpost

2013/1/16

#
'bodies' is your list object and will never be null (though it could be empty). What you intended was 'b', the Body object taken from the list (in lines 7 and 13).
danpost danpost

2013/1/16

#
However, this method could be more simple written as follows:
public void colorChange()
{
    if(!getIntersectingObjects(Body.class).isEmpty())
    {
        setImage(image2);
        play();
    }
    else
    {
        setImage(image1);
    }
}
danpost danpost

2013/1/16

#
In your original code, the 'for' loop iterates through each object that is in the list and checks to see if the object is null, where no actual object is null; so nothing in that code would do what you wanted. In fact, even after replacing 'b' for 'bodies' in lines 7 and 13, the whole method would be equivalent to the following:
public void colorChange()
{
    for(Body b : getIntersectingObjects(Body.class))
    {
        setImage(image2);
        play();
    }
}
which will change its image to 'image2' once it intersects a Body object and its image will never revert back.
kaciedy kaciedy

2013/1/17

#
That makes way more sense than it did before. Thank you a lot!
You need to login to post a reply.