问题
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, .....:)
回答1:
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;
}
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
回答2:
You seem to be trying to get certain keys to activate only once per press. To do that, this code:
public void Update(GameTime gameTime)
{
var now = Keyboard.GetState();
KeyboardState old = Keyboard.GetState();
Should be rewritten as:
private KeyboardState now; // suggest rename to something like mCurrentKeyboardState
private KeyboardState old; // suggest rename to something like mLastKeyboardState
...
public void Update(GameTime gameTime)
{
old = now;
now = Keyboard.GetState();
...
You should also think about putting this input related code into its own class.
来源:https://stackoverflow.com/questions/9889891/xna-menu-not-working