C# C“计时器只在第一时间启动”;如果;
我正在做一个游戏,想展示不同分数的文本。 但此时计时器仅在第一个if(如果分数等于100)时起作用(文本仅显示) 我需要一些帮助,因为它需要与200,300,400-还有1000 代码如下:C# C“计时器只在第一时间启动”;如果;,c#,if-statement,timer,C#,If Statement,Timer,我正在做一个游戏,想展示不同分数的文本。 但此时计时器仅在第一个if(如果分数等于100)时起作用(文本仅显示) 我需要一些帮助,因为它需要与200,300,400-还有1000 代码如下: void startTimer() { if (score == 100) { timerWIN.Start(); } else if (score == 200) { timerWIN.Start();
void startTimer()
{
if (score == 100)
{
timerWIN.Start();
}
else if (score == 200)
{
timerWIN.Start();
}
else if (score == 300)
{
timerWIN.Start();
}
else if (score == 400)
{
timerWIN.Start();
}
else if (score == 500)
{
timerWIN.Start();
}
else if (score == 600)
{
timerWIN.Start();
}
else if (score == 700)
{
timerWIN.Start();
}
else if (score == 900)
{
timerWIN.Start();
}
else if (score == 1000)
{
timer1000.Start();
}
}
private void timerWIN_Tick_1(object sender, EventArgs e)
{
if (timerTick == 1)
{
lblWin1.Visible = true;
lblWin2.Visible = true;
}
else if (timerTick == 15)
{
lblWin1.Visible = false;
lblWin2.Visible = false;
timerWIN.Stop();
}
timerTick++;
}
private void timer1000_Tick(object sender, EventArgs e)
{
if (timerTick == 1)
{
lblWin1.Text = "500 points!";
lblWin2.Text = "You're doing great.";
lblWin1.Visible = true;
lblWin2.Visible = true;
}
else if (timerTick == 15)
{
lblWin1.Visible = false;
lblWin2.Visible = false;
lblWin1.Text = "Yeah that's it.";
lblWin2.Text = "Keep feeding me baby.";
timer1000.Stop();
}
timerTick++;
}
按照要求,以下是我的评分方式:(每次碰撞,我都会得到分数)
private void timer1\u勾选(对象发送方,事件参数e)
{
SnakeCoreLabel.Text=Convert.ToString(分数);
if(down){snake.moveDown();}
if(up){snake.moveUp();}
if(right){snake.moveRight();}
if(左){snake.moveLeft();}
for(int i=0;i
显示正在修改分数的代码片段
您应该按以下方式更改startTimer
方法:
void startTimer()
{
if(score == 1000)
{
timer1000.Start();
}
else if(score % 100 == 0 && score < 1000)
{
timerWIN.Start();
}
}
确保您正在根据您的要求增加分数
。从上面给出的代码来看,分数无处更新
建议:使用下面的代码片段使代码简洁:
void startTimer()
{
if (score >= 100 && score <= 900 && score % 100 == 0)
{
timerWIN.Start();
}
else if (score == 1000)
{
timer1000.Start();
}
}
void startTimer()
{
如果(score>=100&&score我一开始误读了这个问题,您可以通过使用switch语句简化您的代码,它可能会帮助您找到逻辑中的错误,因为从提供的代码中很难看出您到底出了什么错
switch (score)
{
case 100:
case 200:
case 300:
case 400:
case 500:
case 600:
case 700:
case 800:
case 900:
timerWIN.Start();
break;
case 1000:
timer1000.Start();
break;
}
如何调用startTimer?请在将来格式化您自己的代码…您应该使用switch
语句或更简化的if
语句替换megaif
语句。更干净。另外,正确格式化您的代码可能会让您自己发现错误。您听说过或|
声明?您几乎总是启动timerWIN
。它应该是每个分数的一个单独计时器,还是真的应该是timerWIN
高达900,然后timer1000
?我认为如果它所做的只是更新分数,您的代码肯定可以简化为使用一个计时器。我感觉它应该是一个不同的计时器每个分数使用不同的计时器,例如timer100/timer200等。我认为timerWIN
只是一个复制/粘贴作业编号。值100、200、…900都有timerWIN值1000应该有timer1000固定的计时器1000。stopYour else if也会错误地执行0值。根据您的更新,我假设点是一个固定值,为100?不,这是一个10分的固定值,但只有在每100分的情况下,您才能获得标签来显示。好的,那么您是否检查了startTimer
方法中是否有正确的分数?您是否仍然面临同样的问题?
void startTimer()
{
if (score >= 100 && score <= 900 && score % 100 == 0)
{
timerWIN.Start();
}
else if (score == 1000)
{
timer1000.Start();
}
}
switch (score)
{
case 100:
case 200:
case 300:
case 400:
case 500:
case 600:
case 700:
case 800:
case 900:
timerWIN.Start();
break;
case 1000:
timer1000.Start();
break;
}