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

2017/5/10

.setBackground() Is compiling but not executing

WhiteRoseAshes WhiteRoseAshes

2017/5/10

#
Hello, I am trying to make a game over screen appear when the player dies. The code compiles and runs however, the .setBackground() method isn't actually changing the background. The removeObjects method isn't working either. Here's the code I have written:
    public void act() 
    {

        int xpos = getX() - 5;
        if (xpos <= 0) 
        {
            getWorld().removeObject(this);
        }
        else 
        {
            setLocation(xpos, getY());
            Actor player = getOneObjectAtOffset(0,0,RocketPlayer.class);
            if (player != null)
            {
              
                getWorld().removeObjects(getWorld().getObjects(Actor.class));
                getWorld().setBackground(new GreenfootImage("gameOver.jpg"));
                 slow();
                 Greenfoot.setWorld(new MainMenu());
            }
        }

    }
danpost danpost

2017/5/10

#
What does the 'slow' method do?
WhiteRoseAshes WhiteRoseAshes

2017/5/10

#
Slow is a method I created that uses Thread.sleep() Hers the code for that method:
    public static void slow()
    {
        try
        {
            Thread.sleep(5000);
        }
        catch (Exception e)
        {
        }
    }
danpost danpost

2017/5/10

#
Okay, that does not allow the background to be repainted before the MainMenu world becomes active. Try this instead:
public void slow()
{
    int n = 250;
    while (--n > 0) Greenfoot.delay(1);
}
WhiteRoseAshes WhiteRoseAshes

2017/5/10

#
That works! thanks!
danpost danpost

2017/5/10

#
You can probably drop the 'slow' method altogether and replace line 18 above with this:
Greenfoot.delay(250);
You need to login to post a reply.