My game could be described as being similar to flappy bird. My timer keeps count throughout the gameplay, and when the player dies the world changes to the gameOver screen. I want to add some code that states if the player has been alive for > than 30 seconds create the actor for the BronzeMedal in the gameOver world. This would also happen for the silver and gold medals, where the player lives for 1 minute and 1 minute 30 seconds respectively. Im not sure which of my classes is necessary so i will add what i think is :)
Code for my timer
The actor that removes the player and sets the world to GameOver
public class TimerActor extends Actor
{
private boolean running = false;
private int millisElapsed = 0;
private long lastTime = 0;
/**
* Act - do whatever the TimerActor wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
long time = System.currentTimeMillis();
if(lastTime != 0) {
long diff = time - lastTime;
millisElapsed += diff;
}
lastTime = time;
updateImage();
}
public TimerActor() {
updateImage();
}
public void start() {
millisElapsed = 0;
lastTime = 0;
}
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 class Jellyfish extends Actor
{
//Below is all related to animation.
static final int CHG_RATE = 20; // adjust to suit
int chgImgIn = 1;
int imgNum = 1;
String[] images = { "Jellyfish.fw.png", "Jellyfish2.fw.png" };
/**
* Act - do whatever the Jellyfish wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
eat();
animation();
removeTouchingJellyfish();
if (atWorldEdge() == true)
{
getWorld().removeObject(this);
}
}
public void animation()
{
chgImgIn--;
if (chgImgIn == 0)
{
chgImgIn = CHG_RATE; // reset countdown
imgNum = (imgNum + 1) % 2; // this will alternate {0, 1, 0, 1...}
setImage(images[imgNum]);
}
}
/**
* Decided how fast and in what direction the jellyfish move.
*/
public void move()
{
move(-3);
}
public boolean atWorldEdge()
{
if (getX() == 0) {
return true;
} else {
return false;
}
}
public void eat()
{
TimerActor timeractor = (TimerActor) getWorld().getObjects(TimerActor.class).get(0);
Actor shark;
shark = getOneObjectAtOffset(0, 0, Shark.class);
if (shark != null)
{
World world;
world = getWorld();
world.removeObject(shark);
getWorld().removeObject(timeractor);
World newWorld = new GameOver();
Greenfoot.setWorld(newWorld);
GreenfootImage timerImage = timeractor.getImage();
int x = 357-timerImage.getWidth()/2;
int y = 233-timerImage.getHeight()/2;
newWorld.getBackground().drawImage(timerImage, x, y);
}
}
public void removeTouchingJellyfish()
{
Actor jellyfish;
jellyfish = getOneObjectAtOffset(0, 0, Jellyfish.class);
World world;
world = getWorld();
world.removeObject(jellyfish);
}
}

