I have a game that scores you on how long you last. i need to make a counter that counts in seconds and displays this score while you pay.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** IN WORLD SUBCLASS */ // add instance fields private int actCount; // the counter field private Actor timeDisplay; // the time display actor // in constructor timeDisplay = new SimpleActor(); setTimeText(); addObject(timeDisplay, 80 , 20 ); // in act method if (!getObjects(Player. class ).isEmpty()) // only run timer if player in world { actCount++; if (actCount% 55 == 0 ) setTimeText(); } // with added method private void setTimeText() { timeDisplay.setImage( new GreenfootImage( "Time: " +(actCount/ 55 ), 24 , null , null ); } |
1 | public class SimpleActor extends greenfoot.Actor{} |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Sand here. * * @author (your name) * @version (a version number or a date) */ public class Highway extends World { /** * Constructor for objects of class Sand. * */ public Highway() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1100 , 600 , 1 ); prepare(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { int minimum = 320 ; int maximum = 599 ; int randomRangeValue = minimum + Greenfoot.getRandomNumber( maximum - minimum + 1 ); Runner runner = new Runner(); addObject(runner, 35 , Greenfoot.getRandomNumber( 600 )); Ambulance car = new Ambulance(); addObject(car, 158 , randomRangeValue); car.setLocation( 159 , randomRangeValue); Green_Car green_car = new Green_Car(); addObject(green_car, 223 , Greenfoot.getRandomNumber( 280 )); green_car.setLocation( 218 , Greenfoot.getRandomNumber( 280 )); Ambulance car2 = new Ambulance(); addObject(car2, 159 , Greenfoot.getRandomNumber( 280 )); car2.setLocation( 159 , Greenfoot.getRandomNumber( 280 )); End end = new End(); addObject(end, 1079 , 305 ); end.setLocation( 1074 , 300 ); Blue_car blue_car = new Blue_car(); addObject(blue_car, 219 , randomRangeValue); blue_car.setLocation( 217 , randomRangeValue); } |
1 2 3 4 | public void act() { } |