问题描述
我曾尝试在 google 和 stackoverflow 上搜索此内容,但不知道该取什么名字,所以找不到.
I've tried to search for this on google and stackoverflow, but I'm not sure what to call it, so I can't find it.
我将如何为 C# 程序创建一个循环",例如每 100 毫秒运行一次?类似于 Minecraft 所说的滴答声",或者 GameMaker 所说的步数".
How would I make a "loop" for the C# program, that runs every, say, 100 milliseconds? Similar to what Minecraft calls "ticks", or GameMaker calls "steps".
我不知道如何做到这一点.我正在使用 Visual Studio,并且有一个主窗口.有些事情我想不断地执行,所以我想弄清楚如何制作步骤"或更新"功能.
I can't figure out how to do this. I'm using visual studio, and I have a main window. There are things that I want to execute constantly, so I'm trying to figure out how to make a "step" or "update" function.
推荐答案
如果你想让它运行 100 毫秒,你可以这样做
If you want it to run for 100 ms you would do this
System.Timers.Timer timer = new System.Timers.Timer(100);
public void Initialize(object sender, EventArgs e)
{
timer.Elapsed+=Elapsed;
}
public void Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//do stuff
}
你可以做一些其他的事情,但我认为这个没有那么有效
Something else you can do, however I don't think this one is as efficient
using System.Threading;
Thread th = new Thread(new ThreadStart(delegate
{
while (true)
{
Thread.Sleep(100);
//do stuff
}
}));
或者,你可以下载monogame,用c#制作更精致的游戏
Or, you can download monogame to make more elaborate games in c#
它有自己的游戏时间控制.
It has its own gameTime control.
这篇关于我如何制作“循环"?对于游戏,每(数)毫秒运行一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!