I am working on a game, and would really appreciate if anybody could tell me how to add a score counter and a timer. Thanks.
import greenfoot.*; import java.awt.Color; public class Statistic extends Actor { private String caption = ""; private int value; // constructor for a Statistic object public Statistic(String statText) { caption = statText; updateImage(); } // constructor for just displaying a number public Statistic() { this(""); // calls the constructor above } // creates and updates the image of this Statistic object private void updateImage() { String imgText = caption; if (!"".equals(caption)) imgText += ": "; GreenfootImage image = new GreenfootImage(imgText + value, 16, Color.black, new Color(0, 0, 0, 0)); setImage(image); } // Used to set, change or remove the caption public void setCaption(String statText) { caption = statText; updateImage(); } // used to set the value to a specific amount public void setValue(int val) { value = val; updateImage(); } // used to adjust the value (a negative amount reduces the value) public void add(int amt) { value += amt; updateImage(); } // returns the current caption of this Statistic object public String getCaption() { return caption; } // returns the current value of this Statistic object public int getValue() { return value; } }
private Statistic score = new Statistic("Score"); private Statistic timer = new Statistic("Time");