开发者

Artificially slow down time

开发者 https://www.devze.com 2023-03-22 22:21 出处:网络
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.

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 s开发者_运维知识库low 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号