鼠标按钮一直被按下而不是点击
Mouse button keeps being pressed instead of clicking
我目前正在开发一款单人游戏。该游戏主要基于点击按钮,这就是问题所在。每当我按住鼠标左键然后将其移到游戏中的某个按钮上时,该按钮就会立即被点击。
我尝试通过多种方式解决这个问题,通过以不同方式重新排列 if 语句,添加一个额外的 bool 来检查鼠标按钮是否被单击,等等。但没有任何运气。也没有在任何地方找到任何解决方案。
public override void Update(GameTime gameTime)
{
MouseState state = Mouse.GetState();
if (new Rectangle((position - ElementCenter).ToPoint(), sprite.Bounds.Size)
.Contains(state.Position) && oldState.LeftButton == ButtonState.Released)
{
renderColor = Color.LightSlateGray;
if (state.LeftButton == ButtonState.Pressed &&
oldState.LeftButton == ButtonState.Released)
{
switch (button)
{
case "UI/submit":
if (GameWorld.Instance.Team.UserTeamName.Length > 0)
{
GameWorld.Instance.SubmitTeamName();
}
break;
case "UI/teammanager":
GameWorld.Instance.TeamManager();
break;
default:
break;
}
}
}
else
{
renderColor = Color.White;
}
oldState = state;
}
理想情况下,我希望只有在单击按钮之前松开鼠标左键时才会单击按钮。
如果oldState
声明为实例字段,这个class的不同实例将有不同的版本。即,旧状态可能会丢失。
声明 oldState
为 static
!
private static MouseState oldState = Mouse.GetState();
我通常使用这样的class来读取输入
public class InputManager
{
KeyboardState currentKeyboard, previousKeyboard;
MouseState currentMouse, previousMouse;
public bool LeftKeyIsHeldDown { get; private set; }
public bool RightKeyIsHeldDown { get; private set; }
public bool JumpWasJustPressed { get; private set; }
public bool FireWasJustPressed { get; private set; }
public void Update()
{
previousKeyboard = currentKeyboard;
currentKeyboard = Keyboard.GetState();
previousMouse = currentMouse;
currentMouse = Mouse.GetState();
LeftKeyIsHeldDown = currentKeyboard.IsKeyDown(Keys.A);
RightKeyIsHeldDown = currentKeyboard.IsKeyDown(Keys.D);
JumpWasJustPressed = currentKeyboard.IsKeyDown(Keys.Space) && previousKeyboard.IsKeyUp(Keys.Space);
FireWasJustPressed = currentMouse.LeftButton == ButtonState.Pressed && previousMouse.LeftButton == ButtonState.Released;
}
}
我目前正在开发一款单人游戏。该游戏主要基于点击按钮,这就是问题所在。每当我按住鼠标左键然后将其移到游戏中的某个按钮上时,该按钮就会立即被点击。
我尝试通过多种方式解决这个问题,通过以不同方式重新排列 if 语句,添加一个额外的 bool 来检查鼠标按钮是否被单击,等等。但没有任何运气。也没有在任何地方找到任何解决方案。
public override void Update(GameTime gameTime)
{
MouseState state = Mouse.GetState();
if (new Rectangle((position - ElementCenter).ToPoint(), sprite.Bounds.Size)
.Contains(state.Position) && oldState.LeftButton == ButtonState.Released)
{
renderColor = Color.LightSlateGray;
if (state.LeftButton == ButtonState.Pressed &&
oldState.LeftButton == ButtonState.Released)
{
switch (button)
{
case "UI/submit":
if (GameWorld.Instance.Team.UserTeamName.Length > 0)
{
GameWorld.Instance.SubmitTeamName();
}
break;
case "UI/teammanager":
GameWorld.Instance.TeamManager();
break;
default:
break;
}
}
}
else
{
renderColor = Color.White;
}
oldState = state;
}
理想情况下,我希望只有在单击按钮之前松开鼠标左键时才会单击按钮。
如果oldState
声明为实例字段,这个class的不同实例将有不同的版本。即,旧状态可能会丢失。
声明 oldState
为 static
!
private static MouseState oldState = Mouse.GetState();
我通常使用这样的class来读取输入
public class InputManager
{
KeyboardState currentKeyboard, previousKeyboard;
MouseState currentMouse, previousMouse;
public bool LeftKeyIsHeldDown { get; private set; }
public bool RightKeyIsHeldDown { get; private set; }
public bool JumpWasJustPressed { get; private set; }
public bool FireWasJustPressed { get; private set; }
public void Update()
{
previousKeyboard = currentKeyboard;
currentKeyboard = Keyboard.GetState();
previousMouse = currentMouse;
currentMouse = Mouse.GetState();
LeftKeyIsHeldDown = currentKeyboard.IsKeyDown(Keys.A);
RightKeyIsHeldDown = currentKeyboard.IsKeyDown(Keys.D);
JumpWasJustPressed = currentKeyboard.IsKeyDown(Keys.Space) && previousKeyboard.IsKeyUp(Keys.Space);
FireWasJustPressed = currentMouse.LeftButton == ButtonState.Pressed && previousMouse.LeftButton == ButtonState.Released;
}
}