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

2014/1/27

timer stop gameover

JustMe JustMe

2014/1/27

#
Hello everybody, i have a timer in my world that will count down from 20 and when it gets to 0 greenfoot stops. but when it stops i want to add an gameoverscreen. Is there someone that could help me???
Entity1037 Entity1037

2014/1/27

#
Add the game over screen first, then stop greenfoot.
JustMe JustMe

2014/1/27

#
i have an actor gameover with the code: public cc_GameOver() { setImage(new GreenfootImage("Game Over",48, Color.WHITE, Color.BLACK)); } is that also ok?
JustMe JustMe

2014/1/27

#
And this is the code i have in my world class: if (timer>0) { timer--; if (timer==0) Greenfoot.stop(); } addObject(timerText, 95, 25); timerText.setcc_Text("Time left:" + (timer/60)); if (timer%60==0) timerText.setcc_Text("Time left:" + (timer/60));
danpost danpost

2014/1/27

#
Please use the code tag below the 'Post a reply' box for adding code into your posts. Also, you need to give enough information for us to know or be able to determine when the code is executed. The names of the methods the code reside usually helps; often, if called from the act method, its code may help. How and where the various fields are declared could help also. I took the liberty of re-displaying you code properly:
1
2
3
4
5
6
7
8
9
if (timer>0)
{
    timer--;
    if (timer==0) Greenfoot.stop();
}
 
addObject(timerText, 95, 25);
timerText.setcc_Text("Time left:" + (timer/60));
if (timer%60==0) timerText.setcc_Text("Time left:" + (timer/60));
danpost danpost

2014/1/27

#
Line 4 only shows one action being taken when the timer reaches zero -- that is, the scenario stops. That is where you will add your gameover actor into the world. Line 9 appears out of place (like it does not belong with lines 7 and 8, but maybe somewhere else in your code.
JustMe JustMe

2014/1/27

#
oke thank you i will try it right now in the my world cc_Rotterdam i have:
1
2
3
4
5
6
7
8
9
10
11
12
public void act()
{
if (timer>0)
        {
            timer--;
            if (timer==0) Greenfoot.stop();
        }
         
        addObject(timerText, 95, 25);
        timerText.setcc_Text("Time left:" + (timer/60));
        if (timer%60==0) timerText.setcc_Text("Time left:" + (timer/60));
}
and in my cc_GameOver i have:
1
2
3
4
5
6
7
8
9
10
11
12
public class cc_GameOver extends Actor
{
    /**
     * Act - do whatever the cc_GameOver wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public cc_GameOver()
    {
        setImage(new GreenfootImage("Game Over",48, Color.WHITE, Color.BLACK));
    }
    
}
danpost danpost

2014/1/27

#
Ok. With the blank line at line 8 of your act method, your initial post made it seem that you were showing two separate areas of code where in fact you were showing just one. The reason it appeared to be two separate areas of code is because they should be two distinct sets of code. Lines 9 and 10 perform things you only want done once. You want the timerText object to be added to the world once and its image initially set once. Things that you want done just once should not be in the act method (especially without any conditions placed on them being executed). Since these are things that you want done during the construction of your world, they belong in your world constructor. Another thing, line 11 should be executed only immediately after when the value of 'timer' changes; so, it should be in the 'if' block placed after line 5.
JustMe JustMe

2014/1/27

#
thank you danpost i've made the changes. But how can i now get the game over screen when the timer is at 0??? this is what i have now in my public void act() in my world cc_Rotterdam
1
2
3
4
5
6
if (timer>0)
        {
            timer--;
            if (timer%60==0) timerText.setcc_Text("Time left:" + (timer/60));
            if (timer==0) Greenfoot.stop();           
        }
JustMe JustMe

2014/1/27

#
i've come with this: at 0 the words game over will come on my screen
1
2
3
4
5
6
7
8
9
10
if (timer>0)
        {
            timer--;
            if (timer%60==0) timerText.setcc_Text("Time left:" + (timer/60));                  
            if (timer==0)
            {
            addObject(new cc_GameOver(),getWidth()/2, getHeight()/2);
            Greenfoot.stop(); 
            }
        }
Anne2403 Anne2403

2014/11/28

#
Hello.@danpost, @JustMe I been searching in the internet on how to make a game over screen when the time is up and glad I found this but about your last comment, about line 4 and 7,I got syntax error in timerText.setcc_Text, and cc_GameOver() and getWidth().. Why? Can u tell me how can rid of the syntax error? Or something that I can declare? and oh, I put your code in the GameOver() subclass, is that right?
danpost danpost

2014/11/28

#
The timer code should be elsewhere -- probably in your world class. The only thing that you should have in the GameOver class is maybe something to give the actor its image (please review the entire discussion to see where everything led to).
Anne2403 Anne2403

2014/11/28

#
Thank u. @danpost
You need to login to post a reply.