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

2012/5/28

Timer, remove object

Matt Matt

2012/5/28

#
I want to add an object called "LevelOver", use a timer to count about 5 seconds and then to remove "LevelOver" How would I do this
danpost danpost

2012/5/28

#
Create a new class called 'TimedMessage' using the code provided below. Call it from the world class with this:
addObject(new TimedMessage("Level Over", 1000), getWidth() / 2, getHeight() / 2);
The '1000' can be adjusted to increase/decrease the time it is displayed
import greenfoot.*;
// other imports, if needed

public class TimedMessage extends Actor
{
    int cyclesLeft;

    public TimedMessage(String message, int cycles)
    {
        createImage(message);
        cyclesLeft = cycles;
    }

    public void act()
    {
        cyclesLeft--;
        if (cyclesLeft == 0) getWorld().removeObject(this);
    }

    private void createImage(String msg)
    {
        // insert code to create the image you want to display using the text string 'msg' here
    }
}
It is up to you to decide how your message(s) are to look.
You need to login to post a reply.