Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7721137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:50:54+00:00 2026-06-01T03:50:54+00:00

I have a problem with the logic of this code, XNA’s Update Method is

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:50:55+00:00Added an answer on June 1, 2026 at 3:50 am
     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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a legacy code to maintain and while trying to understand the logic
I have this block of code (functions omitted as the logic is part of
I have this code: <%@ taglib uri=http://struts.apache.org/tags-logic prefix=logic%> <% List<String> years = new ArrayList<String>();
I have this code: $um = array(PHP, JAVA, MySQL) $a = count($um) for ($i
This is an odd problem, so I have to provide a bit of background.
I have a problem with django's MVC. I understand that this is not the
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
I have a nice java code that unzips a .zip file. But problem with
I have a problem in running the cron job. here are my code: <?php
im just starting to learn about sockets and i have been given this code,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.