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
And here is the timerText class
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();
}
}
}
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)));
}
}
