![](/img/trans.png)
[英]How do I make a platform go down after a certain amount of time not colliding with the player object in Unity?
[英]How do I use a timer and or a do while loop to make labels show up for a certain amount of time
因此,基本上我现在所拥有的是带有隐藏标签的表单,并且我将案例与简单的计数器(计算图片框的点击次数)一起使用来遍历案例。 我需要做的是,进入第2种情况时,我需要将标签弹出一定的时间(大约1秒钟),并且目标是让用户记住其中的2种并将其放在文本框中。 我只是对如何使用计时器或do while循环使标签仅弹出一定的时间感到困惑,就像在单击时只对标签进行一次检查的情况一样。 另外,我使用枚举存储“答案”,但我不确定100%如何交叉检查用户输入的内容和枚举是什么。 抱歉,如果这是一个非常简单的答案,我对C#还是很陌生,尤其是循环和枚举。 任何帮助将不胜感激。 谢谢!
案例
private void pbMummy_Click(object sender, EventArgs e)
{
counter++;
switch (counter)
{
case 1:
MessageBox.Show("Help me! I lost my passwords. Can you try and just get 2 of them?");
break;
case 2:
lblc1.Visible = true;
lblc2.Visible = true;
lblc3.Visible = true;
lblc4.Visible = true;
lblc5.Visible = true;
System.Threading.Thread.Sleep(1500);
lblc1.Visible = false;
lblc2.Visible = false;
lblc3.Visible = false;
lblc4.Visible = false;
lblc5.Visible = false;
break;
}
}
枚举
public enum Memorize
{
boo92134,
spooky93,
grim432,
fangs9981,
cobweb439
}
public class Mummy
{
public Memorize answer { get; set; }
}
您可以为此使用异步任务,因为这是一个非常简单的任务:
async void ShowLabelForCertainTime( Label label, int milliseconds )
{
label.Visible = true;
await Task.Delay( milliseconds ); //Time in milliseconds
label.Visible = false;
}
只是在需要时调用它:
case 2:
ShowLabelForCertainTime( lbcl1 );
编辑:不确定为什么,但我一直在使用“按钮”而不是“标签”,但原理是相同的
您可以在一段时间后使用计时器将按钮设置为可见。 使用字典,以便您可以检索启动事件的按钮。 这是一个代码示例,作为概念证明,还需要一些重构。
//run this on the click of the button
int button_id = 1; //or whatever way you want to reference your button
//myButtons[button_id].Visible = false;
var btnTimer = new System.Timers.Timer(2000);
btnTimer.Elapsed += myTestClass.HideButton;
btnTimer.Enabled = true;
//keep a reference to that button so you know who the timer belongs to
myTestClass.myTimers[btnTimer] = button_id;
public static class myTestClass
{
public static Dictionary<System.Timers.Timer, int> myTimers = new Dictionary<System.Timers.Timer, int>();
public static void HideButton(object sender, System.Timers.ElapsedEventArgs e)
{
System.Timers.Timer tmr = (System.Timers.Timer)sender;
if(myTimers.ContainsKey(tmr))
{
//here you get your reference back to the button to which this timer belongs, you can show/hide it
//var btn_id = myButtons[myTimers[tmr]];
//myButtons[btn_id].Visible = false;
Console.WriteLine(myTimers[tmr]);
}
tmr.Enabled = false;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.