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

2019/5/10

How to add timer?

hd3377 hd3377

2019/5/10

#
I have made a car game where my car eats coin.I want to add timer of about 100 seconds and if my car fail to eat all coins in 100 seconds the game must stop.How acn we do it guys?
ErasedBlade ErasedBlade

2019/5/10

#
in the act method of which class you want to put this in. i would also create a class for stoping it (a lose or win screen) and coins would be an instance variable that counts how many coins have been collected (lets say there is 100 in total
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void act()
{
int timer = 0;
timer++;
if(timer <= 10000)
{
if(Coins <= 100)
{
new Win()
}
else
{
new Lose()
}
}
}
there is probably other ways to do this as well,but this is how i would do it, i cant remember if there is a method to stop a scenario thats already running. if you would like to use it (if its even there) just replace the new Lose, or new Win with it.
hd3377 hd3377

2019/5/10

#
I want to add timer in a top left screen and if the time is about 100 second the game will stop.I know how to stop the game and all but I just don't know how to display timer.Can you help?
ErasedBlade ErasedBlade

2019/5/10

#
you can either use the showText method in the act method or a separate one being called in the act method, or you can use custom font to display it. I am unable to help with creating the custom font due to I'm in the process of switching my code to custom font from the showText method, and my game is currently 10 times slower then it was before. I have my timer as a instance variable for respawning, and not a local variable. i am not sure know if changing it wont do anything or not. if you want to use the showText method
1
2
3
4
public void text()
{
    showText("Text "+ variable, x, y);
}
replace the text, variable, x, and y, for the values you are using. call this method in the act method so it updates when the value is changed, and if you want it to display on world creation (not a second after), then add it to the constructor also.
Super_Hippo Super_Hippo

2019/5/10

#
For an easy start, import the Counter class. Try to follow its description and come back with questions if you have any.
You need to login to post a reply.