I am making a simulation for a rocket launch , i want to show a countdown and after the countdown, the rocket will move towards the sky. If its possible can u pls tell me how??
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class Timer extends Actor
{
/**
* Act - do whatever the Timer wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private int time = 20;
private int count = 65;
public void act()
{
// Add your action code here.
if(time == 0)
{
return;
}
if(counter())
{
time--;
count = 65;
}
display();
}
private boolean counter()
{
if(count > 0)
{
count--;
}
return count == 0;
}
private void display()
{
setImage(new GreenfootImage("time left before next wave: " + time, 30, Color.WHITE, Color.BLACK));
}
public void setTime()
{
time = 20;
}
public boolean isTimeUp()
{
return time == 0;
}
}/** This class creates a countdown timer object.
* It displays like a digital timer clock.
* The value to set the timer is given in seconds; however, is converted to approximate act cycles within the class.
* All timers created from this class will start automatically unless using the 'Timer(int, boolean)' constructor with boolean set to 'false'.
*/
import greenfoot.*;
import java.awt.Color;
public class Timer extends Actor
{
private int count; // the counter field
private int initialCount; // the initial time given before event occurs
private boolean running;
public Timer()
{
this(0, true);
}
public Timer(int timeBeforeEvent)
{
this(timeBeforeEvent, true);
}
public Timer(int timeBeforeEvent, boolean getsStarted) // int value given in seconds
{
setTimer(timeBeforeEvent);
updateImage();
running = getsStarted;
}
public void setTimer(int timeBeforeEvent)
{
initialCount = 60 * timeBeforeEvent;
count = -initialCount;
}
private void updateImage()
{
String prefix = "T - ";
if (count >= 0) prefix = "T + ";
int time = count * (int)Math.signum(count);
time = time / 60;
int secs = time % 60;
time = (time - secs) / 60;
int mins = time % 60;
int hrs = (time - mins) / 24;
String h = "00"+hrs;
while (h.length() > 2) h = h.substring(1);
String m = "00"+mins;
while (m.length() > 2) m = m.substring(1);
String s = "00" + secs;
while (s.length() > 2) s = s.substring(1);
String text = prefix + h + "h : " + m + "m : " + s + "s";
GreenfootImage textImage = new GreenfootImage(text, 20, Color.black, new Color(0, 0, 0, 0));
GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
image.drawRect(0, 0, image.getWidth()-1, image.getHeight()-1);
image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
setImage(image);
}
public void act()
{
if (running)
{
count++;
if ((count + initialCount) % 60 == 0) updateImage();
}
}
public int getTime()
{
return count / 60;
}
public void start()
{
running = true;
}
public void stop()
{
running = false;
}
}// changed this method
public int getTime()
{
return (count + initialCount) / 60 - initialCount / 60;
}
// added this method
public boolean isRunning()
{
return running;
}