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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:15:24+00:00 2026-06-13T17:15:24+00:00

The problem is, if I hold down one of the arrow keys there is

  • 0

The problem is, if I hold down one of the arrow keys there is a delay between moving constantly. What I mean is if I hold down left, it will move 7 pixels, then a little wit later it moves constantly. can someone tell me how to fix this so it moves “smoothly”

code——————–

package game;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


public class GamePlay implements KeyListener{
 int shipX = 400;
 int shipY = 300;
public int shipSpeed = 7;
public void incShipX(int i) {this.shipX += i;}
public void incShipY(int i) {this.shipY += i;}

Image ship;


File shipFile = new File("C:/Users/Pictures/ship.png");

public GamePlay() {
    try {        
        ship = ImageIO.read(shipFile);

    } catch (IOException ex) {
        Logger.getLogger(GamePlay.class.getName()).log(Level.SEVERE, null, ex);
    }

}

public void drawShip(Graphics g) {  

    g.drawImage(ship, shipX, shipY, null);    

}

@Override
public void keyTyped(KeyEvent ke) {

}

@Override
public void keyPressed(KeyEvent ke) {
    if(ke.getKeyCode() == KeyEvent.VK_LEFT) {
        incShipX(-shipSpeed);
    }

    if(ke.getKeyCode() == KeyEvent.VK_RIGHT) {
        incShipX(shipSpeed);
    }

    if(ke.getKeyCode() == KeyEvent.VK_UP) {
        incShipY(-shipSpeed);
    }

    if(ke.getKeyCode() == KeyEvent.VK_DOWN) {
        incShipY(shipSpeed);
    }
}

@Override
public void keyReleased(KeyEvent ke) {

}

}

I am adding the keyListener to my Canvas—add(gamePlay);

  • 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-13T17:15:25+00:00Added an answer on June 13, 2026 at 5:15 pm

    When you press your arrow key, your operating system only sends a single key pressed event to your Java application. This means pressing the key will only cause your ship to move once.

    If you hold down your key, after a delay, your operating system will start sending repeated key pressed events. If I press and hold my ‘s’ key here… ssssssssssssssssssssss my first ‘s’ appeared immediately, then there was a short delay, and then the rest of the ‘s’ characters came in quickly. This is the same thing happening in your program.

    To correct this, you need to update your ship at a regular interval (or at a variable interval if you account for the time delta when updating the ship). Note an important distinction: a key being pressed happens at an instant of time, whereas the state of the key is a function over time (values being UP or DOWN).

    Not sure if I muddled the issue for you. I can try to explain differently if you require.

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

Sidebar

Related Questions

Is there a way of preventing the menu that appears when you hold down
Running into a problem. I have a table defined to hold the values of
Problem: I have two array where one produce a category and the second produce
Basically when you hold down any of the buttons with the class of block_delete
I need the Button to change the layout when I hold it down for
Problem: Been struggling to get my code to load external shaders and it is
Problem: I've written State Machine for my android application. It is separate class, extension
Problem: I have a table that prints out vertical but I would like it
Problem Given a boolean expression consisting of the symbols 0, 1, &, |, ^
Problem in MATLAB Code for solving desired 'n' number of simultaneous equations of the

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.