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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:39:43+00:00 2026-06-01T12:39:43+00:00

im working on a simple game like space invaders,and i got into a problem.

  • 0

im working on a simple game like “space invaders”,and i got into a problem.
Im trying to give the user, the possiblity to move as much as he want from Left to Right, and in the same time have the possiblty to shot using the “Space bar”.

My problem is: when i pressed more then 1 key, only 1 function run.

here a few stuff i tried:

  1. Storing the keys in a List<Keys> (but i didnt find any good way to excute the functions and everything become messy)

    2.normal handling of the key_down event like this:

    protected void Form1_keysDown(object obj, KeyEventArgs e)
    {
        (e.KeyData == Keys.Space)
            spaceShip.FireBullets();
    
        if (e.KeyCode == Keys.Left)
            spaceShip.MoveLeft();
    
        if (e.KeyCode == Keys.Right)
            spaceShip.MoveRight();
     }
    

my qustion is: what is a good way to make this work?

(sorry for my english)

  • 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-01T12:39:44+00:00Added an answer on June 1, 2026 at 12:39 pm

    You are relying on the keyboard controller repeating the key when you hold it down. That stops working when you press another key. This requires a different approach.

    First you need an enum that indicates the motion state of the spaceship with values like NotMoving, MovingLeft and MovingRight. Add a variable of that type to your class. You’ll need both the KeyDown and KeyUp events. When you get a KeyDown for, say, Keys.Left then set the variable to MovingLeft. When you get the KeyUp event for Keys.Left then first check if the state variable is still MovingLeft and, if it is, change it NotMoving.

    In your game loop, use the variable value to move the spaceship. Some sample code:

        private enum ShipMotionState { NotMoving, MovingLeft, MovingRight };
        private ShipMotionState shipMotion = ShipMotionState.NotMoving;
    
        protected override void OnKeyDown(KeyEventArgs e) {
            if (e.KeyData == Keys.Left)  shipMotion = ShipMotionState.MovingLeft;
            if (e.KeyData == Keys.Right) shipMotion = ShipMotionState.MovingRight;
            base.OnKeyDown(e);
        }
        protected override void OnKeyUp(KeyEventArgs e) {
            if ((e.KeyData == Keys.Left  && shipMotion == ShipMotionState.MovingLeft) ||
                (e.KeyData == Keys.Right && shipMotion == ShipMotionState.MovingRight) {
                shipMotion = ShipMotionState.NotMoving;
            }
            base.OnKeyUp(e);
        }
    
        private void GameLoop_Tick(object sender, EventArgs e) {
            if (shipMotion == ShipMotionState.MovingLeft)  spaceShip.MoveLeft();
            if (shipMotion == ShipMotionState.MovingRight) spaceShip.MoveRight();
            // etc..
        }
    
    • 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 simple board game in qt, and i have some problem
I'm working on a simple little game for the iPhone, and I'd like to
I'm working on a 2D game (kind of like a top down space shooter)
Following this : Best approach for oldschool 2D zelda-like game I got a simple
I'm working on an incredibly simple snake-game in C# using XNA. The problem is
I'm working on a simple game design, and I wanted to break up my
I'm working on a relatively simple 2D side-scrolling iPhone game. The controls are tilt-based.
I have just started trying to make a simple game with XNA 3.1 to
I am working on a simple 2D soccer game, while passing the ball between
I'm new to android game development. I currently working on a simple android app

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.