我的班级有一个程序,当你点击一个按钮时,我们有骰子。
我有代码正常工作,但我正在尝试让骰子旋转数字并以正确的数字“停止”。
我有一个图片框,当你点击按钮时,它会根据随机数生成的内容显示背景图片。
现在,如果我想让第一个骰子翻转20-30张图像然后停止,第二张骰子旋转30-40张图像而不是停止...我将如何实现这一目标?
我试过这样的图像会改变几次......但它显然没有延迟,所以它只显示最后一张图像。
public void DiceTwoDisplay(int _numberTwo)
{
if (_numberTwo == 1)
{
DiceRotate();
imageDie2.BackgroundImage = die1;
}
else if (_numberTwo == 2)
{
DiceRotate();
imageDie2.BackgroundImage = die2;
}
else if (_numberTwo == 3)
{
DiceRotate();
imageDie2.BackgroundImage = die3;
}
else if (_numberTwo == 4)
{
DiceRotate();
imageDie2.BackgroundImage = die4;
}
else if (_numberTwo == 5)
{
imageDie2.BackgroundImage = die5;
}
else if (_numberTwo == 6)
{
DiceRotate();
imageDie2.BackgroundImage = die6;
}
}
public void DiceRotate()
{
imageDie2.BackgroundImage = die1;
imageDie2.BackgroundImage = die2;
imageDie2.BackgroundImage = die3;
imageDie2.BackgroundImage = die4;
imageDie2.BackgroundImage = die5;
imageDie2.BackgroundImage = die6;
}
我希望它快速显示所有这些,然后在最后一个停止。
提前致谢!
答案 0 :(得分:0)
var timer = new Timer(50); // Set up the timer for 50 ms
_timer.Elapsed += new ElapsedEventHandler(timerElapsed);
_timer.Enabled = true; // Enable it
有一些字段代表模具,加上随机数生成器:
Image[] dies = new Image[] { die1, die2, die3, die4, die5, die6 )
Random r = new Random();
你经过的处理程序可能是:
void timerElapsed(object sender, ElapsedEventArgs e)
{
// you have to execute this on UI thread
imageDie2.BackgroundImage = dies[r.Next(5)];//
}
答案 1 :(得分:-1)
你可以简单地添加线程睡眠:
public void DiceRotate()
{
int milliseconds = 2000;
imageDie2.BackgroundImage = die1;
Thread.Sleep(milliseconds);
imageDie2.BackgroundImage = die2;
Thread.Sleep(milliseconds);
imageDie2.BackgroundImage = die3;
imageDie2.BackgroundImage = die4;
imageDie2.BackgroundImage = die5;
imageDie2.BackgroundImage = die6;
}
(或者延迟使用方法或者你想要的w / e)