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

2013/1/13

Timers

camsterb camsterb

2013/1/13

#
I have a timer that counts from 120 down to zero. I use it three times to simulate three rounds in a match. I need to also show a timer counting down from 360 to zero, but this timer must be removed and added to the world again after every 120 seconds with a random time interval inbetween that must not be counted. How can I get this working? I imagine static variables, but I haven't found a way to do it yet.
Oxy Oxy

2013/1/13

#
Could you please post the class code that your question is involved in, we will be able to help you out alot more with that information.
camsterb camsterb

2013/1/13

#
OK, but I just assumed you wouldn't need it, so sorry for not posting it.
import greenfoot.*;  
import java.awt.Color;      
  
public class Timer extends Actor     
{        
   long initialTime = System.currentTimeMillis();  
   long elapsedTime;

   static long totalTime = 360500;
   static long totalElapsedTime = 360500;
   Field field;
   int counter = 0;
    public Timer()     
    {             
        updateImage();
        
    }    
  
    public void act()     
    {      
        elapsedTime = System.currentTimeMillis() - initialTime;  
        updateImage();  
        if (elapsedTime >= 12050) 
        {
            Start start = new Start();
            start.canStart=false;
            showStatistics(); 
        }  
        
    }          
    private void updateImage()  
    {  
        GreenfootImage image = new GreenfootImage(300, 300);
        setImage(image);
        image.drawString("Time: " + ((int) ((120500 - elapsedTime)) / 1000), 108, 20);
        totalTime -= totalTime - elapsedTime;
        image.drawString("Total Time: " + (int) totalTime/1000, 108, 50);
        
    }  
    private void showStatistics()
    {
         getWorld().addObject(new Statistics(), getWorld().getWidth()/2,getWorld().getHeight()/2);
         ScoreBoard scoreboard = new ScoreBoard();
         scoreboard.period();
         getWorld().removeObject(this);
    }
} 
I would like totalTime to show on the screen a countdown timer from 360. It will be removed from the world when it hits 240, then added again when a new timer object is created. It must then continue from 240, before being removed again at 120, and so on.
vonmeth vonmeth

2013/1/14

#
Why not just recreate a new '360' timer each time, but set the timer based off what round it is (round 1 set to 360, round 2 set it to 240, etc.) then you don't have to worry about that "random time interval inbetween that must not be counted."
camsterb camsterb

2013/1/14

#
Yeah, that's what I did! I just wonder if there is a real way to do it.
You need to login to post a reply.