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

2018/3/9

10 Second Timer

Atebits Atebits

2018/3/9

#
Im looking to make a basic 10 second timer in my game. The code goes along the lines of whats given below, my issue is knowing how long a second is in this fashion, is it 60 for running at 60fps? Or is it something other? Thanks in Advanced. :) import greenfoot.*; public class Timer extends Actor { //set variable private int time = 250; public void act() { //run timer time --; //reset timer while (time == 0) { time = 250; } } }
danpost danpost

2018/3/9

#
Atebits wrote...
Im looking to make a basic 10 second timer in my game. The code goes along the lines of whats given below, my issue is knowing how long a second is in this fashion, is it 60 for running at 60fps? Or is it something other? << Code Omitted >>
Actually, it varies depending on the speed of your system. However, in general, at normal running speed, it is around 60 fps. Therefore, initializing your 'time' field to '600' would be about right. Sidenote: it is a bit strange to see a 'while' where an 'if' is sufficient. You could also code act method as follows:
public void act()
{
    time = (++time)%600;
}
where your 'time' field is initialized to '0'. The value will reset to '0' every 600 frames.
You need to login to post a reply.