This site requires JavaScript, please enable it in your browser!
Greenfoot back
vtn2@calvin.edu
vtn2@calvin.edu wrote ...

2014/7/10

Unusual behavior when I clear the background

vtn2@calvin.edu vtn2@calvin.edu

2014/7/10

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class girl extends Actor
{
    int count = 0;
    public girl() 
    {
        setRotation(45);
    }
    
    /**
     * Act - do whatever the girl wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        count++;
        if (count % 10 == 0) {
            System.out.println("Clearing the background");
            GreenfootImage img = getWorld().getBackground();
            img.clear();
            getWorld().setBackground(img);
        } 
        move(10);
    }    
}
When I run this code, which I do by clicking "Act" repeatedly, I get some funny behavior, which makes me think there might be a bug in Greenfoot. What happens is that the girl moves 10 "units" each time until the 10th time. At this point, count is 10, so the background is cleared -- which doesn't make anything look different because I never put anything in the background anyway. However, from there on out, each time the girl moves 10, a copy of her image is left on the background. Also, after the 10th act(), if I click on the girl image with the mouse and move her on the canvas, there are repeated copies of the image left on the screen. Is this a bug in greenfoot? If not, can someone explain what I'm seeing? (Also, if I do getWorld().getBackground(), does that give me a *copy* of the background image or is it the same image? I.e., do I really need to clear it and set it back again with setBackground(), or could I just clear the image?) Thanks. Vic
danpost danpost

2014/7/10

#
No, that is not a bug. When you use 'getWorld().getBackground()', you get a reference to the world canvas -- the actual background image. It is called a background for a reason -- so you do not see beyond it. You should ensure that it remain completely opaque. If you wish to clear the background image, you should use 'getWorld().getBackground().fill()', which will paint the background white (unless you changed the drawing color for that image).
You need to login to post a reply.