os i am working for a project and the goal is to survive 60 seconds in the game, as you can see in the end that i tried something but didnt work :/ any help will be appreciated.
import greenfoot.*;
/**
* Write a description of class Counter1 here.
*
* @author
* @version 1.0.0
*/
public class Counter1 extends Actor {
private boolean running = false;
private int millisElapsed = 0;
private long lastTime = 0;
public Counter1() {
updateImage();
}
public void start() {
millisElapsed = 0;
lastTime = 0;
}
public void gamePaused() {
lastTime = 0;
}
public void act() {
long time = System.currentTimeMillis();
if(lastTime != 0) {
long diff = time - lastTime;
millisElapsed += diff;
}
lastTime = time;
updateImage();
win();
}
public void updateImage() {
// Calculate minutes & seconds elapsed
int millis = millisElapsed % 1000;
int secs = (millisElapsed / 1000) % 60;
int mins = millisElapsed / 60000;
// Convert these into text
String millisText = String.format("%03d", millis);
String secsText = String.format("%02d", secs);
String minsText = "" + mins;
String text = minsText + ":" + secsText + "." + millisText;
// Update the image
GreenfootImage img = new GreenfootImage(text, 25, null, null);
setImage(img);
}
public void win()
{
if (millisElapsed == 60000)
{
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
}