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

2015/3/4

Help Displaying a Message and then Removing it

shooterbooth shooterbooth

2015/3/4

#
I want to display a message when you run out of clicks and can no longer add a ball in the world. I want it to briefly appear and then disappear. The message I am referring to is message3 but it won't disappear with this code. Thanks in advance!
public void gameEnd()
    {
        Message message= new Message("Gameover! You ran out of time!");
        Message message2= new Message("Final Score: " + score.getPoints());
        Message message3= new Message("You ran out of clicks!");
        if (time.getPoints()==0)
        {
            Greenfoot.stop();
            addObject(message, getWidth()/2, getHeight()/2-20);
            addObject(message2, getWidth()/2, getHeight()/2+20);
        }
        if (clicks.getPoints()==0 && clickMessageTime <=20)
        {
            clickMessageTime++;
            addObject(message3, getWidth()/2, getHeight()/2);
            if (clickMessageTime>=20)
            {
                removeObject(message3);
            }
        }
    }
Super_Hippo Super_Hippo

2015/3/4

#
You are calling the gameEnd method and the message is added. But then, you want to have the timer there to remove it. This would only work if you would call this method all the time or it would never reach 20. Probably you do that. What happens is that you have 20 of these messages above each other (every time you call the method, another message is added) and you only remove the one you just added. Instead, you have to save message3 in a class field (like 'private Message message3=new Message("You ran out of clicks!");') so you don't lose the reference to it when the method is exited. So in the end, it could look like this:
    private Message message3 = new Message("You ran out of clicks!");
    private int clickMessageTime = -2;

    public void act()
    {
        if (clickMessageTime > 0) clickMessageTime--;
        else if (clickMessageTime==0) {clickMessageTime--; removeObject(message3);}
    }

    public void gameEnd()
    {
        if (time.getPoints()==0)
        {
            Greenfoot.stop();
            addObject(new Message("Gameover! You ran out of time!"), getWidth()/2, getHeight()/2-20);
            addObject(new Message("Final Score: " + score.getPoints()), getWidth()/2, getHeight()/2+20);
        }
        if (clicks.getPoints()==0) && clickMessageTime==-2) //it will only happen once like this. I don't know how you want it.
        {
            clickMessageTime=50;
            addObject(message3, getWidth()/2, getHeight()/2);
        }
    }
shooterbooth shooterbooth

2015/3/4

#
Thanks! After you said that I realized that in all of my older programs that's what I did and I never had a problem.
danpost danpost

2015/3/4

#
An easier way for the temporary message is to subclass the Message class with a TempMessage class with the timer int field and an act method to run the timer and remove itself when the timer is exhausted. That would keep the code cleaner in the class above and well as clean timer code.
public class TempMessage extends Message
{
    private int timer;

    public TempMessage(String text)
    {
        super(text);
    }

    public void act()
    {
        timer++;
        if (timer == 50) getWorld().removeObject(this);
    }
}
Then, to adjust the code Super_Hippo gave:
private Message message3 = new TempMessage("You ran out of clicks!");
 
public void gameEnd()
{
    if (time.getPoints()==0)
    {
        Greenfoot.stop();
        addObject(new Message("Gameover! You ran out of time!"), getWidth()/2, getHeight()/2-20);
        addObject(new Message("Final Score: " + score.getPoints()), getWidth()/2, getHeight()/2+20);
    }
    if (message3 != null && clicks.getPoints()==0) 
    {
        addObject(message3, getWidth()/2, getHeight()/2);
        message3 = null;
    }
}
You need to login to post a reply.