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

2013/9/20

How to remove text from the world after a few seconds

NikitaVarma NikitaVarma

2013/9/20

#
I have created a text class where I use the method in it to display text messages on the screen after validating certain conditions. I want the text to disappear from the screen after a few seconds so that I can continue with the remaining functionality. I have the following code for Text class
public class Text extends Actor
{
    public Text()  
    {  
        this("");  
    }  

    public Text(String text)  
    {          setText(text);  
    }  

    public void setText(String text)  
    {  
        setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0)));  
    }   

    
}
And this is one instance where I use the Text class
if(name.equals("Penny"))
            {
                Text errormsg=new Text();
                getWorld().addObject(errormsg, 600, 220);
                errormsg.setText("You cannot take coke with a penny");
                Greenfoot.stop();
            }
I want this message to disappear. How do I do it?
randeepsingh85 randeepsingh85

2013/9/20

#
Try : Greenfoot.delay(3000);
danpost danpost

2013/9/20

#
randeepsingh85 wrote...
Try : Greenfoot.delay(3000);
I think that would be a bit too long of a time to display the message. Try somewhere between 80 and 200.
randeepsingh85 randeepsingh85

2013/9/20

#
Thanks danpost.
NikitaVarma NikitaVarma

2013/9/20

#
The delay method only causes the text to be displayed a bit late, I have another text message to be displayed on the same location. I have to clear the text in order to display the new one @danpost: any idea on how to do it?
danpost danpost

2013/9/20

#
First display the message, then use the delay; then clear the message (or present a new one).
NikitaVarma NikitaVarma

2013/9/20

#
Thanks! It worked
You need to login to post a reply.