【发布时间】:2014-10-09 15:34:36
【问题描述】:
对不起,如果这个问题太简单了。我正在尝试复制匹配游戏教程
http://msdn.microsoft.com/en-us/library/dd553234.aspx
但我使用自定义按钮(名为 Cards)来显示图像。一张卡片有两个图像(背面,正面)。所有卡片的背面都是相同的,“正面”是随机分配图片的图像。将图像设置为背面隐藏随机图像,正面显示它。如果两次连续单击具有相同类型的图像(匹配),我希望单击事件在不触发计时器的情况下结束,但似乎无论如何都会触发计时器。这是按钮单击事件中的代码。我尝试在 if() 中使用 firstClicked.front 而不是 firstClicked.Image 但似乎没有任何效果。谢谢
private void card_Click(object sender, EventArgs e)
{
if (timer1.Enabled == true)
return;
Card clickedCard = sender as Card;
if (clickedCard != null)
{
if (clickedCard.Image == clickedCard.front) //ingore Clicks on already revealed cared
return;
if (firstClicked == null)
{
firstClicked = clickedCard;
firstClicked.Image = firstClicked.front;
return;
}
//Clicked card is second card
secondClicked = clickedCard;
secondClicked.Image = secondClicked.front;
if (firstClicked.Image == secondClicked.Image)
{
firstClicked = null;
secondClicked = null;
return;
}
timer1.Start();
}
}
【问题讨论】:
-
调试你的代码。你输入最后一个 if 吗?
-
调试器在哪里说它失败了?
-
首先不要使用 == true 或 == false。你也知道退货是如何运作的吗?他们退出他们所在的方法。有效地停止他们在该方法中运行的所有内容。当前,如果启用 timer1,则不会运行其他任何内容。
-
@deathismyfriend:“永远不要使用 == true 或 == false”?为什么不?这是一个风格的事情。一点冗余也没什么不好。使用大括号括住单个语句的顺序。
-
@JimMischel 我认为这是因为看到人们也写
myBool != false