问题描述
我对这段代码的逻辑有问题,XNA的Update Method速度很快,因此,当我每次按下键盘上的向下按钮试图将值增加1时,最终更新的不仅仅是by 1
I have a problem with the logic of this code, XNA's Update Method is to fast so when i am trying to icreas the value by 1 every time the down button is pressed on the keyboard, it ends up updating a more than just by 1
看看代码,看看是否可以想到一种更好的方法,
Have a look at the code and see if you can think of a better way to do it,
public void Update(GameTime gameTime)
{
var now = Keyboard.GetState();
KeyboardState old = Keyboard.GetState();
if (now.IsKeyDown(Keys.Down) && !old.IsKeyUp(Keys.Down))
{
properties.Menuposition++;
}
else if (now.IsKeyDown(Keys.Up) && !old.IsKeyUp(Keys.Up))
{
properties.Menuposition--;
}
else if (now.IsKeyDown(Keys.Enter))
{
properties.Menuposition = 5;
}
old = now;
}
这是更新方法,这是绘制方法
That was the update method and this is the Draw Method
public void Draw(GameTime gametime, SpriteBatch spriteBatch)
{
if(properties.Menuposition == 0)
{
spriteBatch.DrawString(properties.Font, properties.Menu[0], properties.Playpos, Color.White);
spriteBatch.DrawString(properties.Font, properties.Menu[1], properties.Highscorepos, Color.White);
spriteBatch.DrawString(properties.Font, properties.Menu[2], properties.Exitpos, Color.White);
}
int menueitem = 0;
Vector2 play = new Vector2(320,117);
Vector2 highscore = new Vector2(320, 151);
Vector2 Exit = new Vector2(320,180);
spriteBatch.DrawString(properties.Font, properties.Menuposition.ToString(),new Vector2(100,100),Color.White);
if(properties.Menuposition == 1)
{
spriteBatch.DrawString(properties.Font, properties.Menu[0], play, Color.Yellow);
spriteBatch.DrawString(properties.Font, properties.Menu[1], highscore, Color.White);
spriteBatch.DrawString(properties.Font, properties.Menu[2], Exit, Color.White);
numberoftime = true;
menueitem = 1;
}
else if(properties.Menuposition == 2)
{
spriteBatch.DrawString(properties.Font, properties.Menu[0], play, Color.White);
spriteBatch.DrawString(properties.Font, properties.Menu[1], highscore, Color.Yellow);
spriteBatch.DrawString(properties.Font, properties.Menu[2], Exit, Color.White);
numberoftime = true;
menueitem = 2;
}
else if(properties.Menuposition == 3)
{
spriteBatch.DrawString(properties.Font, properties.Menu[0], play, Color.White);
spriteBatch.DrawString(properties.Font, properties.Menu[1], highscore, Color.White);
spriteBatch.DrawString(properties.Font, properties.Menu[2], Exit, Color.Yellow);
numberoftime = true;
menueitem = 3;
}
我正在考虑另一种方法,但这意味着我会更改所有代码,所以看看它,看看是否有更好的方法
I am thinking of another way to do it but it would mean i would have to change all the code so have a look at it and see if you have a better way of doing this
感谢和问候,
..... :)
Thanks and Regards,.....:)
推荐答案
private float timeSinceLastPush = 0;
public void Update(GameTime gameTime)
{
var now = Keyboard.GetState();
timeSinceLastPush +=(float)gameTime.ElapsedGameTime.TotalSeconds;
KeyboardState old = Keyboard.GetState();
if (now.IsKeyDown(Keys.Down) && !old.IsKeyUp(Keys.Down)&& timeSinceLastPush >0.5 )
{
properties.Menuposition++;
timeSinceLastPush = 0;
}
else if (now.IsKeyDown(Keys.Up) && !old.IsKeyUp(Keys.Up)&& timeSinceLastPush >0.5)
{
properties.Menuposition--;
timeSinceLastPush = 0;
}
else if (now.IsKeyDown(Keys.Enter)&& timeSinceLastPush >0.5)
{
properties.Menuposition = 5;
timeSinceLastPush = 0;
}
old = now;
}
我添加了一个变量float来保存自上次以来的时间经过了0.5秒的命令并重置
I added a variable float to hold the time elapsed since last time if it has passed 0.5 seconds do the comand and reset
这篇关于XNA菜单不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!