2012-03-27 60 views
0

我有这个代码的逻辑问题,XNA的更新方法是快速的,所以当我试图每次在键盘上按下按钮时将值加1时,它最终更新不仅仅是由1XNA菜单不工作

看一看代码,看看,如果你能想到的一个更好的办法来做到这一点更,

 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; 
    } 

这是更新的方法,这是Draw方法

 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; 
     } 

我是t hinking另一种方式来做到这一点,但这意味着我将不得不改变所有的代码,所以看看它,看看你是否有更好的方式做到这一点

感谢和问候, ..... :)

回答

0
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; 
      } 

我加一个变量浮动举行自去年经过的时间,如果它已经过去了0.5秒做的COMAND和复位

1

你似乎在试图让某些键激活只有一次每按一次。要做到这一点,这样的代码:

public void Update(GameTime gameTime) 
{ 
    var now = Keyboard.GetState(); 
    KeyboardState old = Keyboard.GetState(); 

应改写为:

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(); 
    ... 

你也应该考虑把该输入相关代码到自己的类。