This site requires JavaScript, please enable it in your browser!
Greenfoot back
vtn2@calvin.edu
vtn2@calvin.edu wrote ...

2014/6/13

Is there any way to convert between game speed and time?

vtn2@calvin.edu vtn2@calvin.edu

2014/6/13

#
I'm looking for an approximate way to convert between the game "speed" and time. FYI: the game speed can be set via an API, and the values are between 0 and 100, and it is considered a percentage. I'm trying to create an API called wait(secs) where secs is a float. When the game is running at 100% speed a call to wait(secs) would cause the Actor to do nothing for secs seconds. E.g., a call to wait(1.5) would cause the actor to do nothing for 1.5 seconds (approximately). It does not have to be super accurate -- just "close enough". I looked at the source code of greenfoot and it seems like there is a correlation between 30000 nanoseconds and one percent of the speed of the game. The code is pretty complicated so I'm not sure that is correct. Does anyone have any ideas on how I could implement this api? Thanks. Vic
danpost danpost

2014/6/13

#
With the following, it would not matter how fast the scenario was running. One the other hand, it would matter how slow the scenario was running. If the wait time was shorter than the time between act cycles, it would act as if the wait was not there. If the wait was only a few times the time between act cycles, the accuracy will be reduced. Basically, if you run the scenario at standard speed (around 50 -- or the middle of the slide bar, which is between 50 and 60 acts per second) or faster, then you should be ok for waits of '0.1f' or more.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// add these fields
private long startTime;
private long pauseDuration;
// put condition on executing code in act method
public void act()
{
    if (!paused())
    {
        // any code originally in act
    }
}
 
// method to initiate having actor hold up for allotted time
public void wait(float duration)
{
    pauseDuration = (long)(duration*1000f);
    startTime = System.currentTimeMillis();
}
 
//  method to return paused state
private boolean paused()
{
    return System.currentTimeMillis() < startTime+pauseDuration;
}
vtn2@calvin.edu vtn2@calvin.edu

2014/6/16

#
Thanks, danpost, for your response. This worked very well for me.
You need to login to post a reply.