0

如果我尝试做一个循环,它将无法在 XNA 中工作。我想用它来选择角色(格斗游戏),如果您按下一个键(选择一个角色),它将添加到当前计数,然后如果该计数等于某个数字,则会显示一条消息显示两个字符都被选中。那是为了防止玩家选择/加载超过一定数量的角色进入战斗竞技场。就我而言,我只希望加载 2 个字符而不再加载。

我的代码是这样的:

int count = 1;

if (int i = 0; i < count; i++)
   count = count + i;

if (Keyboard.GetState().IsKeyDown(Keys.A))   // This will select Character A
   count += 1;
if (Keyboard.GetState().IsKeyDown(Keys.D))   // This will select Character B
   count += 1;

if (count == 2)   // This checks to see if the total count has reached 2
   // Message is displayed here

有人有什么想法吗?

4

1 回答 1

1

You have a syntax error here (and it seems this for-loop-like construct isn't even necessary. What is it's purpose? if (int i = 0; i < count; i++)

You're code is a logical mess so I won't try to build on it in my answer. I don't think I understand everything but here's an attempt.

You need state which is simply variables that indicate whether something has been loaded or no. You can simply use two boolean variables as follows:

//at the Game class level:
bool char1Selected = false;
bool char2Selected = false;

//inside Update(GameTime)
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
   char1Selected = true;
}

if (Keyboard.GetState().IsKeyDown(Keys.D))
{
   char2Selected = true;
}

if(char2Selected && char1Selected)
{
    //then both are selected. do something here according to your game's logic.
}
于 2012-10-06T02:12:41.633 回答