[英]Main Menu navigation / keyboard input speed is too fast
我最近开始使用XNA游戏工作室4.0制作视频游戏。 我使用按钮列表制作了一个包含4种精灵字体的主菜单。 当我按下向上和向下箭头时,它们会将颜色从白色变为黄色。
我的问题是,当我滚动它时,从顶部字体到底部字体非常快,直接到最后一个字体。 我不确定为什么会这样? 是因为我把它放在更新方法中并且它每60秒左右调用一次?
这是我按箭头键时的代码。
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0)
{
selected--;
}
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1)
{
selected++;
}
}
keyboard = prevKeyboard;
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyUp(key));
}
我需要有人帮助我减速到合理的速度。
如果你能帮助我,我将不胜感激。
我认为问题是因为您没有正确设置prevKeyboard
。
尝试这个:
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0)
{
selected--;
}
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1)
{
selected++;
}
}
prevKeyboard = keyboard; // <=========== CHANGE MADE HERE
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyUp(key));
}
我认为是因为
if (CheckKeyboard(Keys.Up))
{
if (selected > 0)
{
selected--;
// This loops executes so quick before you release button.
// Make changes here to stop the loop if the button is pressed and loop
// executed once.(May be) just **return;** would do ?
}
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1)
{
selected++;
// This loops executes so quick before you release button.
// Make changes here to stop the loop if the button is pressed and loop
// executed once.(May be) just **return;** would do ?
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.