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 9002277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:32:57+00:00 2026-06-16T00:32:57+00:00

I am working on a game tutorial with XNA but I am modifying it

  • 0

I am working on a game tutorial with XNA but I am modifying it slightly. Instead of adding projectiles to an array based on a duration, I am trying to add them to the array by pressing the SpaceBar. So this way the user has some control over firing the missiles.

  if (currentKeyboardState.IsKeyDown(Keys.Space))
  {
      AddProjectile(player.Position + new Vector2(player.Width / 2, 0));
  }



   private void AddProjectile(Vector2 position)
   {
       Projectile projectile = new Projectile();
       projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
       projectiles.Add(projectile);
   }

However I am running into a minor problem. Since I am using the method IsKeyDown() missiles will be added to the projectiles array until the space bar is released. Is there a method that will register a key press rather than IsKeyDown().

IsKeyDown

  • 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-16T00:32:58+00:00Added an answer on June 16, 2026 at 12:32 am

    Add a field that keeps track of the previous keyboard state

    KeyboardState _previousKeyboardState = Keyboard.GetState();
    

    You will update this state at the end of the Update. You can now do edge detection:

    KeyboardState currentKeyboardState = Keyboard.GetState();
    if(_previousKeyboardState.IsKeyUp(Keys.Space) 
       && currentKeyboardState.IsKeyDown(Keys.Space))
    {
        //action
    }
    _previousKeyboardState = currentKeyboardState; //update previous state
    

    The thinking is: If on previous pass the key had not been pressed, but now it is pressed, do this action.

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

Sidebar

Related Questions

I'm working on a game, but I'm having trouble trying to set the scores
I'm trying to make a fully-automated game engine which needs to be working based
I am working on game application and i use AnimationDrawable for image change. but
I've been trying to get this Sudoku game working, and I am still failing
I'm working on board game based on playing cards, and following is the prototype
I have just started trying to make a simple game with XNA 3.1 to
I'm working on an RPG-style game in XNA and I'm working on implementing a
I'm currently working on a tile-based game in Java2D, and I was thinking of
I am working on a tile based, top-down 2D game with dinamically generated terrain,
so I am working on a game, following a tutorial online. Currently I have

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.