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

2013/10/30

decrementing a timer

i_joker123 i_joker123

2013/10/30

#
Im trying to create a timer which decrements and the game should stop once the timer hits 0 but my timer decrements to fast, i think it decrements in milliseconds, i would like it to decrement in seconds. : Any suggestions
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class baddieWorld extends World
{
    Counter counter = new Counter("Baddie score: ");
    TimerText timerText= new TimerText();
    private int timer =40;
    private int score;
    
    public void countPop()
    {
        
        counter.add(1);
        
    }
    public balloonWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        timerText.setText("Time left: " + (timer));  
        
        
        populate();
        
    }
    private void populate()
    {
        
        addObject(timerText, 100, 15); //wherever  
        addObject(counter, 300,50);
        counter.getValue();
        
        
    }
    public void act() 
    {
        if(Greenfoot.getRandomNumber(100) < 3) {
            addObject(new balloon(), Greenfoot.getRandomNumber(700), 600);   
        }
        if (timer>0)  
        {  
            timer--;  
         
                timerText.setText("Time left: " + (timer));  
            if(timer == 0) 
               Greenfoot.stop();  
        } 
        
    }
    
   
}
And here is the timerText class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color; 
/**
 * Write a description of class TimerText here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TimerText extends Actor
{
    /**
     * Act - do whatever the TimerText wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }   
   
    public TimerText()  
    {  
        this("");  
    }  
  
    public TimerText(String text)  
    {  
        setText(text);  
    }  
  
    public void setText(String text)  
    {  
        setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0)));  
    }   
}  
danpost danpost

2013/10/31

#
The timer, as you run it here, is counting act cycles (or frames). An average rate for frames per second is between 50 and 60. So you need to start your timer around 2000 (40 * 50) and display its value divided by 50.
i_joker123 i_joker123

2013/10/31

#
rather than decrementing from 2000 how do i get it to slowly decrement in seconds from 20?
danpost danpost

2013/10/31

#
i_joker123 wrote...
rather than decrementing from 2000 how do i get it to slowly decrement in seconds from 20?
You would then have to use two timers; one to count the frames and one to count approximate seconds.
// instance fields
private int frameTimer, secondTimer = 20;
TimerText timerText = new TimerText();
// to run timers
frameTimer = (frameTimer+1)%50;
if (frameTimer == 0)
{
    secondTimer--;
    timerText.setText("Time left: " + (secondTimer))
    if (secondTimer == 0) Greenfoot.stop();
}
i_joker123 i_joker123

2013/11/2

#
okie dokie, many thanks:)
You need to login to post a reply.