C# 使变量正确递增的问题
我正在为我最后一年的项目制作一个教育游戏,当我的玩家框与另一个物品框发生碰撞时,我遇到了一个增加值的问题。当我的播放机死机后重新设置时,会出现错误,第一个项目收集得很好,但是程序似乎会跳过下一个if语句,并提取第三个项目,而不是第二个项目 我在下面添加了我认为存在问题的代码 LevelManager.csC# 使变量正确递增的问题,c#,xna,C#,Xna,我正在为我最后一年的项目制作一个教育游戏,当我的玩家框与另一个物品框发生碰撞时,我遇到了一个增加值的问题。当我的播放机死机后重新设置时,会出现错误,第一个项目收集得很好,但是程序似乎会跳过下一个if语句,并提取第三个项目,而不是第二个项目 我在下面添加了我认为存在问题的代码 LevelManager.cs for (int x = hints.Count - 1; x >= 0; x--) { hint
for (int x = hints.Count - 1; x >= 0; x--)
{
hints[x].Update(gameTime);
if (player.CollisionRectangle.Intersects(
hints[x].CollisionRectangle))
{
hints.RemoveAt(x);
player.Hint++;
}
}
游戏1.cs
if ((gameState == GameState.Playing) ||
(gameState == GameState.PlayerDead) ||
(gameState == GameState.GameOver))
{
spriteBatch.Begin(
SpriteSortMode.BackToFront,
BlendState.AlphaBlend);
TileMap.Draw(spriteBatch);
player.Draw(spriteBatch);
LevelManager.Draw(spriteBatch);
spriteBatch.DrawString(
pericles8,
"Score: " + player.Score.ToString(),
scorePosition,
Color.White);
spriteBatch.DrawString(
pericles8,
"Lives Remaining: " + player.LivesRemaining.ToString(),
livesPosition,
Color.White);
if (player.Hint == 1)
{
spriteBatch.DrawString(
pericles8,
"Hint: " + "Antivirus is needed to protect your PC",
HintLocation,
Color.Black);
}
if (player.Hint == 2)
{
spriteBatch.DrawString(
pericles8,
"Hint: " + "When the first virus was created, it changed the name of floppy disks that were infected",
HintLocation,
Color.Black);
}
if (player.Hint == 3)
{
spriteBatch.DrawString(
pericles8,
"Hint: " + "Blue Whale is NOT the name of a virus and also whilst adware is annoying, it is not considered a virus",
HintLocation,
Color.Black);
}
if (player.Hint == 4)
{
spriteBatch.DrawString(
pericles8,
"Hint: " + "By having a firewall up, you lock all backdoors into your PC." + "\n" + "Also by staying clear of sites you dont know, you lower the chance of viruses getting into your PC" + "\n"
+ "However you should install relevant protection to increase your chance of not getting viruses",
HintLocation,
Color.Black);
}
spriteBatch.End();
}
if (gameState == GameState.PlayerDead)
{
player.Hint = 0;
}
如能提供任何帮助,我们将不胜感激
干杯我在您提供的代码片段中没有看到bug。我所能说的就是你可以在线路上设置断点
player.Hint++;
(或者更好地在提示属性的setter中设置事件)并查看它何时以及为什么更改为3。这不是答案,应该是注释。玩家可能在同一循环中击中两个对象,因此它将增加两倍。尝试在
player.Hint++之后放置循环中断代码>我已经从你的问题标题中删除了标签,因为大家的一致意见是否定的,它们不应该。不相关,但你似乎太相信防火墙了。