Hello, I have to integrate a timer in my game, from which 15 seconds expire. I've been trying to create this timer for almost 2 hours, but it's not working the way I imagine, and slowly confusing myself with what I programmed.
This is the code frome the actor-class Timer:
and this from the world Level2:
Can someone help me and tell exactly where my mistakes are?
public class Timer extends Actor
{
int time = 15;
private static final Color transparent = new Color(0,0,0,0);
/**
* Act - do whatever the Timer wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Timer()
{
updateImage();
}
public void updateImage()
{
setImage(new GreenfootImage("Time: " + time, 30, Color.BLACK, transparent));
}
}public class Level2 extends MyWorld
{
Lives lives = new Lives();
Timer timer = new Timer();
int time = 15;
/**
* Constructor for objects of class Level2.
*
*/
public Level2()
{
prepare();
}
public Lives getLives()
{
return lives;
}
public Timer getTimer()
{
return timer;
}
private void prepare()
{
getLives();
addObject(lives, 730, 40);
getTimer();
addObject(timer, getWidth() / 2, 40);
addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
addObject( new Lemon(), Greenfoot.getRandomNumber(700)+50, 0);
}
public void act()
{
timeup();
}
public void timeup()
{
Timer timer = new Timer();
((Level2)getWorld()).getTimer().addTime(-1);
}
}

