Errm...让我们看看...我的项目遇到的错误是...奇怪。
我正在制作一个怪物训练游戏,当它进入战斗模式时,可能会出现一个错误,一旦你按下控制左侧或右侧的键,然后再进入战斗。
这个错误很简单,例如,如果你按下 A(又名左键),当它让你选择你将要采取的行动时,游戏会一直识别好像仍然按下 A 按钮,即使它不是。
这是导致错误的代码:
public static int ChooseAction()
{
int choosen = 0;
Battle.CanAct = true;
Battle.ChoosenAction = -1;
while (Battle.ChoosenAction == -1)
{
if (Battle.KeyDelay == 0)
{
if (Procedures.ButtonPressed(Procedures.KeyCodes.KeyRight))
{
Battle.KeyDelay = 64;
int a = Battle.SelectedAction;
a++;
if (a >= BattleActions.Length)
{
a -= BattleActions.Length;
}
Battle.SelectedAction = a;
}
else if (Procedures.ButtonPressed(Procedures.KeyCodes.KeyLeft))
{
Battle.KeyDelay = 64;
int a = Battle.SelectedAction;
a--;
if (a < 0)
{
a += BattleActions.Length;
}
Battle.SelectedAction = a;
}
else if (Procedures.ButtonPressed(Procedures.KeyCodes.Action))
{
Battle.ChoosenAction = Battle.SelectedAction;
}
}
if (KeyDelay != 0 && !Procedures.ButtonPressed(Procedures.KeyCodes.KeyRight) && !Procedures.ButtonPressed(Procedures.KeyCodes.KeyLeft))
{
KeyDelay = 0;
}
if (Battle.KeyDelay > 0)
{
Battle.KeyDelay--;
}
}
choosen = Battle.ChoosenAction;
Battle.CanAct = false;
return choosen;
}
我不知道这是否会有所帮助,但该脚本在线程上运行,因为它是根据官方脚本的修改制作的,在 c# 控制台中。
此外,ButtonPress 过程在按钮被按下时返回,但每次调用它都会创建一个新的布尔值。
任何可能导致该错误的线索?
Procedures.ButtonPress(KeyCodes) 脚本。
public static bool ButtonPressed(KeyCodes buttonKey)
{
bool pressed = false;
switch (buttonKey)
{
case KeyCodes.MenuKey:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.M) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.Start))
pressed = true;
break;
case KeyCodes.RunKey:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.LeftShift) || Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.RightShift) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.B))
pressed = true;
break;
case KeyCodes.KeyUp:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.W) || Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadUp))
pressed = true;
break;
case KeyCodes.KeyDown:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S) || Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadDown))
pressed = true;
break;
case KeyCodes.KeyLeft:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A) || Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadLeft))
pressed = true;
break;
case KeyCodes.KeyRight:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.D) || Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.DPadRight))
pressed = true;
break;
case KeyCodes.Action:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.A))
pressed = true;
break;
case KeyCodes.Return:
if (Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Microsoft.Xna.Framework.Input.Buttons.B))
pressed = true;
break;
}
return pressed;
}