C# 中仅在特定时间触发事件
我正在用 C# 编写一个视频游戏,我想仅在游戏循环中的特定点处理某些事件(例如键盘/鼠标事件)。例如,有没有办法编写如下内容:
void gameLoop()
{
// do updates
handleAllEvents();
render();
}
如果事件发生在循环中的其他点,我想等到 handleAllEvents()
来处理它们。我所说的“事件”是指标准 C# 事件系统,例如:
public event Action<object, KeyboardEventArgs> KeyPressed;
如果问题表述不清楚,请告诉我。谢谢!
I am writing a video game in C#, and I would like to handle certain events (e.g. keyboard/mouse events) only at a specific point in my game loop. For example, is there a way to write something like the following:
void gameLoop()
{
// do updates
handleAllEvents();
render();
}
If an event occurs at some other point in the loop, I would like to wait until handleAllEvents()
to handle them. By "events" I mean the standard C# event system, for example:
public event Action<object, KeyboardEventArgs> KeyPressed;
Please let me know if the question is not phrased clearly. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
删除对
Application.Run()
的调用,并构建您自己的消息循环。例如这样:然后您必须确保渲染不会在那里停留太久。
有关详细信息,我建议您研究
Application
类,特别是方法Run()
和DoEvents()
。Remove the call to
Application.Run()
, and build up your own message loop. For example like this:You must then ensure, that render will not stay there for so long.
For more info I suggest you to study the
Application
class, especially methodsRun()
andDoEvents()
.如果您使用游戏循环,通常不会处理事件,您会在需要时轮询输入设备(即在
handleAllEvents()
方法中)。一些快速研究告诉我,在 opentk 你可以找到这些东西位于OpenTK.Input
命名空间中,特别是KeyboardDevice
类中。If you're using a game loop you generally wouldn't handle events, you'd poll input devices when you need them (i.e. in your
handleAllEvents()
method). Some quick researched told me that in opentk you can find this stuff in theOpenTK.Input
namespace and in particular theKeyboardDevice
class.我相信在 OpenTK 中执行此操作的正确方法是从
GameWindow
类继承,然后重写OnUpdateFrame
和OnRenderFrame
。当您签出主干时,其中包含一个快速入门解决方案,请检查Game.cs
文件。[编辑]为了进一步澄清,
OpenTK.GameWindow< /code>
类提供了一个
Keyboard
属性(类型为 OpenTK.Input.KeyboardDevice),应在OnUpdateFrame
。无需单独处理键盘事件,因为这已经由基类处理。另外,要获得更详细的示例,请从其网页下载另一个入门工具包。该类应该看起来像这样:
我建议您下载 Yask 并检查它是如何实现的。
I believe the proper way to do it in OpenTK would be to inherit from the
GameWindow
class, and then overrideOnUpdateFrame
andOnRenderFrame
. There is a QuickStart solution included when you checkout the trunk, check theGame.cs
file.[Edit] To clarify further,
OpenTK.GameWindow
class provides aKeyboard
property (of type OpenTK.Input.KeyboardDevice), which should be read insideOnUpdateFrame
. There is no need to handle keyboard events separately, as this is already handled by the base class.Also, for a more elaborate example, download Yet another starter kit from their webpage. The class should look something like this:
I would recommend that you download Yask and check how it's implemented there.