简体   繁体   中英

Artificially slow down time

So in my game I have:

seconds = ((elapsed  - now) / 1.0E9F);

Where elapsed and now are System.nanoTime(); called at different places in the code.

Then I convert this into a string and show it to the users. Based off of this code, I also have a countdown.

I want to slow down this countdown on occasion but I can't conceptualize how I'd do that.

Can anyone help me?

This is not Java, but might help you as anyways C# syntax ain't much different from java :P

http://blog.gallusgames.com/programming/a-custom-game-clock-in-csharp

You should introduce an interface like Clock that has a nanoTime method. Have one implementation that simply wraps the system clock, another that allows you to set the time to some arbitrary time. Pass whatever instance you need into the countdown code.

Your code should refer to System.nanoTime() in one place and one place only. During testing you can intercept the timing calls in this one place. Now you can tick once per 10 real ticks, or set time to an arbitrary value and re-set it during tests, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM