简体   繁体   English

人为减慢时间

[英]Artificially slow down time

So in my game I have: 所以在我的游戏中我有:

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

Where elapsed and now are System.nanoTime(); elapsednowSystem.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 这不是Java,但可能会帮助你,因为无论如何C#语法与java:P没有太大的不同

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

You should introduce an interface like Clock that has a nanoTime method. 你应该引入一个像具有nanoTime方法的Clock这样的界面。 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. 您的代码应该在一个地方和一个地方引用System.nanoTime()。 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. 现在,您可以每10个实际滴答一次,或将时间设置为任意值,并在测试期间重新设置等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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