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

2019/5/6

I Need Help with a Count Down Timer

chrishmas chrishmas

2019/5/6

#
I'm working on a game as a project for school and it requires a timer. I have the timer made and I have it showing in the world. However, when I run the program, the timer does not show it counting down in the world. The timer counts down but I need it to show it counting down in the world. Any help would be greatly appreciated.
danpost danpost

2019/5/6

#
chrishmas wrote...
the timer does not show it counting down in the world. The timer counts down but I need it to show it counting down in the world.
Sounds like you have two different timer fields (probably one in the World subclass and one in the Counter class).
chrishmas chrishmas

2019/5/6

#
danpost wrote...
chrishmas wrote...
the timer does not show it counting down in the world. The timer counts down but I need it to show it counting down in the world.
Sounds like you have two different timer fields (probably one in the World subclass and one in the Counter class).
I only have a timer in the world, here's what I have. Like I said, in the top left corner of my world I see "Timer: " but there's no time in it and it doesn't show it counting down. But I know the timer I made is counting down because my game will end after a certain amount of time. Also, thank you for helping, really panicking over here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public MyWorld()
{   
    // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
    super(560, 560, 1);
    prepare();
    showTime();
    act();
}
 
private int time = 1100;
 
private void showTime()
{
    showText("Time: ", 100, 100);
    time--;
}
 
 
public void act()
{
    time--;
    if (time == 0)
    {
        Greenfoot.playSound("ocarina-of-time-73-game-over.mp3");
        Greenfoot.stop();
    }
}
danpost danpost

2019/5/6

#
chrishmas wrote...
I only have a timer in the world, here's what I have. Like I said, in the top left corner of my world I see "Timer: " but there's no time in it and it doesn't show it counting down. But I know the timer I made is counting down because my game will end after a certain amount of time. << Code Omitted >>
Your code to show that text is on line 14. It is only coded to show "Time: ". You need to include (add) the timer (time) to it. Also, you need to call the method from the act method for it to update. You can remove line 15 as that just makes your timer start at 1099.
chrishmas chrishmas

2019/5/6

#
Which part do I call if I already have the act() part up there? I made the changes you said as well.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(560, 560, 1);
        prepare();
        showTime();
        act();
    }
     
    private int time = 1100;
     
    private void showTime()
    {
        showText("Time:1100", 100, 100);
    }
    
     
    public void act()
    {
        time--;
        if (time == 0)
        {
            Greenfoot.playSound("ocarina-of-time-73-game-over.mp3");
            Greenfoot.stop();
        }
    }
danpost danpost

2019/5/7

#
Remove line 14. Change line 21 so you show a variable time (not a literal one). Use "Time: "+time. Add showTime(); to the act method.
chrishmas chrishmas

2019/5/7

#
danpost wrote...
Remove line 14. Change line 21 so you show a variable time (not a literal one). Use "Time: "+time. Add showTime(); to the act method.
Okay, I made those changes. The time still won't count down on screen though. Surely I have something else wrong or missing, just not sure what it is.
danpost danpost

2019/5/7

#
chrishmas wrote...
The time still won't count down on screen though. Surely I have something else wrong or missing, just not sure what it is.
Show your revised code.
chrishmas chrishmas

2019/5/7

#
danpost wrote...
chrishmas wrote...
The time still won't count down on screen though. Surely I have something else wrong or missing, just not sure what it is.
Show your revised code.
Here public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(560, 560, 1); prepare(); showTime(); } private int time = 1100; private void showTime() { showText("Time: "+time, 100, 100); } public void act() { time--; if (time == 0) { Greenfoot.playSound("ocarina-of-time-73-game-over.mp3"); Greenfoot.stop(); } }
danpost danpost

2019/5/7

#
danpost wrote...
Add showTime(); to the act method.
chrishmas chrishmas

2019/5/7

#
danpost wrote...
danpost wrote...
Add showTime(); to the act method.
That worked! Thank you so much! :)
You need to login to post a reply.