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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:18:02+00:00 2026-06-03T04:18:02+00:00

I found a very good tutorial about how to create games with Java2D. In

  • 0

I found a very good tutorial about how to create games with Java2D. In this tutorial there is a section called ‘Moving sprites’ that shows how to move a little spacecraft image on the screen. I was playing with the craft and realized that it was “blocking” on screen sometimes. After some tests and thinking about the problem I found out that the blocking problem was happening because when you use the arrows of the keyboard sometimes you press Left and Right at same time for example, and this blocks the movement.

So my question is: how do I handle this kind of keyboard event – when you are pressing LEFT button keep pressing it and then press RIGHT button – so the character movement is not blocked?

I think this is very common to happen because when you are playing you switch from left to right but for an instant you are pressing left and right at the same time.

  • 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-03T04:18:03+00:00Added an answer on June 3, 2026 at 4:18 am

    You can do this in multiple ways. One way is to use booleans in your program.

    You can set booleans to true when you press a certain key and to false when you release them. ie:

    int x,xSpeed;
    
    boolean movingLeft  = false;
    boolean movingRight = false;
    
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_LEFT){
            movingLeft = true;
        }
        if(key == KeyEvent.VK_RIGHT){
            movingRight = true;
        } 
    }
    
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if(key == KeyEvent.VK_LEFT){
            movingLeft = false;
        }
        if(key == KeyEvent.VK_RIGHT){
            movingRight = false;
        } 
    }
    
    public void moving(){
        if(movingLeft){
            x -= xSpeed;
        }
        if(movingRight){
            x += xSpeed;
        }
    }
    

    Ok let’s look at this code. Because we’re using the ‘else if’ you either move right, or you move left. I haven’t test this code so not quite sure if this will work 🙂

    Let me know what you think and good luck!

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

Sidebar

Related Questions

I am a java programmer, I found that Java is very good at doing
I know that this is a repeated question. I have found very similar questions
I've been looking at the code in this tutorial , and I found that
I've found a very good article about how to use EntLib Validation Block for
I've found I think very good tutorial on how to compile gcc on windows
I'm trying to find a good XCode 4 tutorial out there that does the
Does anybody know any good video tutorial for Emacs (basics)? I found very good
I'm interested in learning this language, but it seems that there is very few
I haven't found a very good answer to my problem so I'm starting a
One thing I have continually found very confusing about using an object database like

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.