我正在尝试制作一个非常简单的逻辑游戏。这个想法是看到一个带有一定数量的彩色方块(按钮)的矩阵,然后隐藏它们,玩家必须单击彩色方块。所以我需要在绘制方块/按钮和返回原始颜色之间有 2 秒的延迟。所有代码都在一个中实现button_click
event.
private void button10_Click(object sender, EventArgs e)
{
int[,] tempMatrix = new int[3, 3];
tempMatrix = MakeMatrix();
tempMatrix = SetDifferentValues(tempMatrix);
SetButtonColor(tempMatrix, 8);
if (true)
{
Thread.Sleep(1000);
// ReturnButtonsDefaultColor();
}
ReturnButtonsDefaultColor();
Thread.Sleep(2000);
tempMatrix = ResetTempMatrix(tempMatrix);
}
这是整个代码,但我需要的是在调用之间有一些延迟SetButtonColor()
and ReturnButtonsDefaultColor()
。我所有的实验Thread.Sleep()
到目前为止还没有成功。我有时会遇到延迟,但彩色方块/按钮从未显示。
您看不到按钮改变颜色,因为Sleep
call 会阻止消息被处理。
处理这个问题最简单的方法可能是使用计时器。使用 2 秒延迟初始化计时器,并确保默认情况下禁用它。然后,您的按钮单击代码将启用计时器。像这样:
private void button10_Click(object sender, EventArgs e)
{
// do stuff here
SetButtonColor(...);
timer1.Enabled = true; // enables the timer. The Elapsed event will occur in 2 seconds
}
以及计时器的 Elapsed 事件处理程序:
private void timer1_TIck(object sender, EventArgs e)
{
timer1.Enabled = false;
ResetButtonsDefaultColor();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)