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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 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; } } |
1 2 | private Statistic score = new Statistic( "Score" ); private Statistic timer = new Statistic( "Time" ); |