在不使用 Thread.sleep() 的情况下进行等待? [英] Make something wait without using Thread.sleep()?

查看:28
本文介绍了在不使用 Thread.sleep() 的情况下进行等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一些字符串来介绍游戏,我希望一个人等待另一个弹出,并且我不想直接使用 Thread.sleep() 让它等待,因为我是不确定这是否是最好的选择.除了让线程休眠之外,还有没有其他方法可以让某些东西等待,或者我只需要让线程休眠?

I am trying to make an intro to a game with some strings that I want one to wait for another to pop up, and I don't directly want to use Thread.sleep() for it to wait, because I am not sure if that is the best option. Is there any other way to make something wait than making the thread sleep, or will I just have to make the thread sleep?

推荐答案

如果这是一个游戏,你不应该使用睡眠或计时器.

If this is a game you shouldn't use sleeps or timers.

通常游戏有自己的内部时钟机制.这意味着您将尝试尽可能快地渲染帧.您的 OnRender 方法将使用游戏的当前时间被调用.您可以使用它来确定是否有足够的时间进入下一个屏幕.

Typically games have their own internal clock mechanism. This means you will try to render the frames as fast as possible. Your OnRender method will be invoked with the current time of the game. You can use this to determine if enough time has passed to go to the next screen.

这意味着您将在第 1 帧中获得一个时间点 A.您将在第 2 帧中获得 Delta 或另一个时间点 B.您可以通过使用 delta 或计算自己做增量.这是一种非常有效的计时机制,并且在单线程游戏时效果很好.任何程序的理念都是永远不要阻塞任何东西.

This means you will be given a point in time A in frame 1. You'll be given the Delta or another point in time B in frame 2. You can determine how much time has passed by using the delta or calculating the delta yourself. This is a very efficient mechanism for timing situations and worked quite well when games were single threaded. The idea of any program is to never block for anything.

通常阻塞的原因是 I/O,例如从磁盘读取、网络或将数据放在 GPU 上.在您的情况下,您可以无障碍地做任何事情.

The reasons things typically block is due to I/O such as reading from disk, the network or putting data on the GPU. In your situation you can do everything without blocking.

这是一个关于这个https://gamedev的不错的页面.stackexchange.com/questions/1589/fixed-time-step-vs-variable-time-step

这篇关于在不使用 Thread.sleep() 的情况下进行等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆