问题描述
if (bool232 = true)
{
if (gameTime.TotalGameTime.TotalSeconds >= 3)
{
this.Exit();
}
}
else
{
}
出于某种原因,这段代码让我的游戏在3秒后退出,尽管bool232是假的,bool232唯一的时间是真的是当屏幕上的游戏显示时
任何帮助?
For some reason this code is making my game exit after 3 seconds despite "bool232" being false, the only time bool232 is true is when the game over screen is shown
any help?
推荐答案
if (bool232 == true)
甚至更好:
or even better:
if (bool232)
(这个布尔值的更好名称也不会受到影响。)
(A better name for that boolean wouldn't hurt, either.)
if (bool232 == true)
{
if (gameTime.TotalGameTime.TotalSeconds >= 3)
{
this.Exit();
}
}
else
{
}
你在里面分配一个值if语句而不是比较。代码甚至不应该编译。
You are assigning a value inside the if statement instead of comparing. The code should not even compile.
if (bool232)
优于
is better than
if (bool232 == true)
虽然如果你打算使用==一个好习惯(有时叫做yoda代码)就是总是把常数放在第一位
though if you are going to use the == a good practice (sometimes called yoda code) is to always put the constant first
if (true == bool232)
这样,如果忘记'='符号,你总会遇到编译错误。
that way you will always get a compile error if you forget an '=' sign.
这篇关于为什么这段代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!