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

2015/12/8

need help with timer

shane727 shane727

2015/12/8

#
I have a timer made already that counts down in milliseconds. The timer starts at 0 and counts down but is there any way i can have the timer start at 7 and go down from there? Beginner to coding
Super_Hippo Super_Hippo

2015/12/8

#
Show the code that you have. Will be much easier to change a small bit then.
shane727 shane727

2015/12/8

#
public class timerMinus extends Actor
{
    int time=07;
   
    /**
     * Act - do whatever the counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */  
    long pauseTime = 10; 
    public boolean running()
    {
        long time = System.currentTimeMillis();
        return time > pauseTime;
    }
    
    public void pause(long delay )
    {
        long time = System.currentTimeMillis();
        pauseTime = time+delay;
    }
    
    public void act() 
    {
        setImage(new GreenfootImage("Time : " + time/100 + " sec", 24, Color.WHITE, Color.BLACK));
        if(running())
        { 
           time = time-1;
        }
    }  
}
Super_Hippo Super_Hippo

2015/12/8

#
Change line 3 to this:
int time=799;
You display 'time/100', not 'time'.
danpost danpost

2015/12/9

#
The initial value of '10' or '799' for 'time' is basically irrelevant. Once 'running' is called in line 25, the value of 'time' is set to some huge value that the value of 'pauseTime' will never be close to matching; and decrementing the value of 'time' (line 27) is inconsequential. You said this timer counts down in milliseconds. Well, if your scenario is running at normal speeds, then the change in milliseconds will be about 17 per act cycle. If you are wanting to convert to seconds, you need to divide milliseconds by 1000. You would need to keep a reference to a starting time or the ending time and use the difference between that and the current time (divided by 1000) to give the value to display.
shane727 shane727

2015/12/9

#
@Super_Hippo thanks for the help! now the timer starts at 7 and counts down!
You need to login to post a reply.