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.
/** 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);
}public class SimpleActor extends greenfoot.Actor{}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);
}
public void act()
{
}