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

2018/3/12

How to make a timer?

1
2
FlorianZhu FlorianZhu

2018/3/12

#
I'm a beginner and I just want to make a timer which should count the time until the game is stopped. I've already learnt some of the ways in the Discussions, but honestly I'm still not understand. So if anyone would be patient to me and show me the way I'll be very happy.
Yehuda Yehuda

2018/3/12

#
If you want your game to last 1000 act cycles then you would create an int variable and set it equal to 1000. Every act cycle should subtract 1 from it. When it reaches 0 the game is over.
...
private int timer = 1000;
...
public void act() {
    timer--;
    if (timer <= 0) {
        Greenfoot.stop(); // pause the execution of the program if 'timer' is less than or equal to 0
    }
}
FlorianZhu FlorianZhu

2018/3/13

#
thank you but I don't want a countdown timer, I would like to have a timer that show how long the game takes. Any suggestions?
Super_Hippo Super_Hippo

2018/3/13

#
As the easiest way, you could add this line after line 5:
showText(""+timer, 50, 50); //change the (50|50) to the coordinates where it should appear
danpost danpost

2018/3/13

#
Please see my Value Display Tutorial. It contains exactly what you want in the section Using a Simple Actor Object. You may need to look over the first two sections to fully understand what is going on with it.
Yehuda Yehuda

2018/3/14

#
Do you want a stopwatch to time the total duration of the game, and then at the end display the amount of act cycles that have elapsed since the beginning? Or, do you want a timer to restrict the game to a specific amount of time/act cycles.
FlorianZhu FlorianZhu

2018/3/14

#
Sorry about the " disappearing", just a little bit school things to do.
Yehuda Yehuda

2018/3/14

#
I don't know what you mean by: " disappearing" Is your problem all solved now, or no improvement since last time?
danpost danpost

2018/3/14

#
Yehuda wrote...
I don't know what you mean by: " disappearing" Is your problem all solved now, or no improvement since last time?
I think what was meant is that it took a while for any type of response due to excess school work that apparently had priority. Also, possibly that nothing has yet been attempted to resolve the issue.
Yehuda Yehuda

2018/3/15

#
Meaning that we'll be gotten back to after there has been time to try something(./?)
FlorianZhu FlorianZhu

2018/3/15

#
Hello. I'm here again to learn and enjoy the Greenfoot.
FlorianZhu FlorianZhu

2018/3/15

#
Super_Hippo wrote...
As the easiest way, you could add this line after line 5:
showText(""+timer, 50, 50); //change the (50|50) to the coordinates where it should appear
I‘ve tried what you said, and it worked. But not exactly what I want it to look like. Well I would like to have a little clock in my programm to show the time from the Beginning to the end(well the duration of the game). And it should look like this (for example): 01(minute):59(second):36(millisecond)and the speed of the "minute part " should be logically slower than the "second part". here is my code: Fields private int timer =0 Constructors (nichts) Methods public void act() timer=timer+1 getWorld().showText(""+timer,30,30) if(timer==10000) Greenfoot.stop() What should I do next?
FlorianZhu FlorianZhu

2018/3/15

#
danpost wrote...
Please see my Value Display Tutorial. It contains exactly what you want in the section Using a Simple Actor Object. You may need to look over the first two sections to fully understand what is going on with it.
Thanks. Still reading.
danpost danpost

2018/3/15

#
danpost wrote...
Please see my Value Display Tutorial.
Apparently, I screwed up the link. The scenario is my Value Display Tutorial (with proper link).
Super_Hippo Super_Hippo

2018/3/15

#
You can try this to display minutes and seconds: (I partially copied it from one of my scenario, so some of it is German. But maybe that's not a problem, because I saw you using the word "nichts" in your last post.) ;)
//everything in active World subclass
private final int FPS = 60;
private Uhr uhr = new Uhr();
private int[] uhrzeit = {0, 0}; //Stunde + Minute
private int timer = 0;

//in constructor
addObject(uhr, 50, 50);

//in act
if (++timer==FPS)
{
   timer = 0;
   if (++uhrzeit[1]==60)
   {
       if (++uhrzeit[0]==24)
       {
           uhrzeit[0]=0;
       }
       uhrzeit[1]=0;
   }
   uhr.updateImage();
}

//as an inner class
public class Uhr extends Actor
{
    public Uhr()
    {
        updateImage();
    }
      
    public void updateImage()
    {
        String h=uhrzeit[0]<10?"0"+uhrzeit[0]:""+uhrzeit[0], min=uhrzeit[1]<10?"0"+uhrzeit[1]:""+uhrzeit[1];
        GreenfootImage img = new GreenfootImage(50, 20);
        img.setColor(Color.BLACK);
        img.setFont(new Font("Calibri", 20));
        img.drawString(h+":"+min, 5, 18);
        setImage(img);
    }
}
There are more replies on the next page.
1
2