This site requires JavaScript, please enable it in your browser!
Greenfoot back
danbyization
danbyization wrote ...

2013/1/16

Timer to stop game after 60 seconds or something

1
2
danbyization danbyization

2013/1/16

#
Hi Is it possible to create a timer that will stop the game after 60 seconds or a specified time limit. If it is possible then how can i achieve this?
danpost danpost

2013/1/16

#
Add an instance int field, called 'timer' to the world class with:
private timer = 0;
As 60 cycles per second is an average for most scenarios, 3600 would approximate 60 seconds or something. The value can be adjusted as neccessary. In the world 'act' method, place the following code:
if (timer>0)
{
    timer--;
    if(timer == 0) Greenfoot.stop();
}
The timer can be set to its start value (3600 or whatever) either in the world constructor (if it is to start immediately upon the scenario being started) or somewhere else (if there is a trigger programmed in your scenario to start the timer -- like a keystroke being detected, a button being clicked on, or a secondary timer used as a delay before starting the game timer). To clarify, this method of timing a game uses actual game-time, not real-time, for the timer.
fajrizarmy fajrizarmy

2013/1/16

#
@danpost : error... where i can put " private timer = 0; " ??
fajrizarmy fajrizarmy

2013/1/16

#
private timer = 0;
did you mean
int timer = 0;
danpost danpost

2013/1/16

#
No, but I did mean:
private int timer = 0;
(the combination of the two)
danbyization danbyization

2013/1/16

#
Thank you for the replies Is there anyway to make it so that the time remaining will show on screen??
Gevater_Tod4711 Gevater_Tod4711

2013/1/16

#
You can use the demo I uploaded before a view minutes.
danbyization danbyization

2013/1/16

#
Thanks Gevater_Tod4711 But i would rather create one myself i just need a plain text one.
Gevater_Tod4711 Gevater_Tod4711

2013/1/16

#
You could use the method drawTime() to see how the time is printet onto an image.
danbyization danbyization

2013/1/16

#
How do i use that method??
danpost danpost

2013/1/16

#
What you could do is create a Text class and update its image (text string) as time progresses. A simple Text class would be:
import greenfoot.*;
import java.awt.Color;

public class Text extends Actor
{
    public Text()
    {
        this("");
    }

    public Text(String text)
    {
        setText(text);
    }

    public void setText(String text)
    {
        setImage(new GreenfootImage(text, 24, Color.black, new Color(0, 0, 0, 0)));
    } 
}
Then, in your sub-class of World, add an instance Text field and create it and add the text object into the world in your world constructor, or a method it calls (like 'prepare'), and add the line to update its image.
// add world instance field
Text timerText = new Text();
// in the constructor (or a method it calls)
addObject(timerText, 100, 15); //wherever
timerText.setText("Time left: " + (timer/60));
// insert after 'timer--;' in the 'act' method (or a method it calls)
if (timer%60==0) timerText.setText("Time left: " + (timer/60));
danbyization danbyization

2013/1/17

#
You've confused me now danpost i have the timer in my greenfoot game but i want to be able to show the user how much time they have left to complete the game
danpost danpost

2013/1/17

#
I understand that. If you look at lines 5 and 7 of the last code provided, your 'timer' variable is being used to determine what the Text object is to display. The Text class is a means of creating an object that display text. The initial text to display is passed to it in the constructor parameter, if wanted. New text can be passed to the Text object by way of the 'setText' method. So, as your 'timer' variable changes (see line 7 above) the text is updated to the new remaining time left.
danbyization danbyization

2013/1/17

#
It wont let me add the line
Text timerText = new Text(); 
into my subclass of world
danpost danpost

2013/1/17

#
First make sure you created a new sub-class of Actor called 'Text' and replace any code within that class with the 20-line code above. Then make sure that the line you are having trouble with is located where specified below (with the question marks being the name of your sub-class of world)
public class ???????? extends World
{
    Text timerText=new Text();
    // other fields

    public ????????()
    {
        super(...// etc.
There are more replies on the next page.
1
2