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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:10:01+00:00 2026-06-02T00:10:01+00:00

I’m coding a game and I’m using Java’s Swing. And right now i’m trying

  • 0

I’m coding a game and I’m using Java’s Swing. And right now i’m trying to get the KeyListeners and Action listeners to work.

What I’m trying to do is to make my object to move according to what key i’m pressing. (Left,Right,Up,Down), But for some reason nothing happens when i press either of these keys, but when i press 3 of them at the same time. the object is strangely moving to the left..

So here’s my code for the class to create the Runner-object:

import java.awt.*;


public class Runner{
    private int xpos, ypos, base, side;

    public Runner(int b, int h ) {
        base = b;
        side = h;
    }
    public void setPosition(int x, int y){
        xpos = x;
        ypos = y;
    }
    public void view(Graphics g) {
        int x[] = { xpos, xpos-base/2, xpos + base/2};
        int y[] = { ypos, ypos + side, ypos + side };
        g.setColor(Color.lightGray);
        g.fillPolygon( x, y, 3 );
        g.setColor(Color.darkGray);
        g.drawLine(xpos, ypos, xpos, ypos + side);
    }
    public void shoot(Graphics g){
        g.setColor(Color.red);
        g.drawLine(xpos,ypos, xpos, 0);
    }
}

And here’s the code thats suppose to run the damn thing:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class RunningGame extends JPanel implements KeyListener, ActionListener{
    Runner rs;
    int x,y;
    Timer t;
    boolean shot = false;
    boolean left = false, right = false, up = false, down = false;

    public RunningGame() {
        x = 100;
        y = 150;
        rs = new Runner(40,60);
        rs.setPosition(x,y);
        this.addKeyListener(this);
        this.setBackground(Color.black);
        t = new Timer(40, this);
        t.start();
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        rs.view(g);
        if(shot) rs.shoot(g);
    }
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == 37) {left = true;}
        if (e.getKeyCode() == 39) {right = true;}
        if (e.getKeyCode() == 38) {up = true;}
        if (e.getKeyCode() == 40) {down = true;}
        if (e.getKeyCode() == 32) {shot = true;}

        rs.setPosition(x,y);
        this.repaint();
}
    public void keyReleased(KeyEvent e){
        if (e.getKeyCode() == 37) left = false;
        if (e.getKeyCode() == 39) right = false;
        if (e.getKeyCode() == 38) up = false;
        if (e.getKeyCode() == 40) down = false;
        if (e.getKeyCode() == 32) shot = false;
        this.repaint();
    }
public void keyTyped(KeyEvent e){}
    public void actionPerformed(ActionEvent e) {
        if (left) {
            if(right){
                right = false;
                x = x - 10; shot = false;
            }
        }
        if (right) {
            if(left){
            left = false;
            x = x + 10; shot = false;
            }
        }
        if (up) {
            if(down){
                down = false;
                y = y - 10; shot = false;
            }
        }
        if (down) {
            if(up){
                up = false;
                y = y + 10; shot = false;
            }
        }
        rs.setPosition(x,y);
        this.repaint();
}

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300); f.setLocation(100,100);
        f.setTitle("Running");
        RunningGame p = new RunningGame();
        f.add(p); f.setVisible(true);
        p.requestFocus();

    }
}

(This is not the final code it’s just using an example with a spaceship, later i will use a different object, just wanna test the KeyListener and ActionListener so it works before proceeding.)

Anyways can anyone help me make the space ship move smoothly? and without having to release all keys to activate another? i.e If i hold left i want it to be able to press another button. so that if i press right, the space ship will start to move in that direction instead.

//MrElephants

  • 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-02T00:10:02+00:00Added an answer on June 2, 2026 at 12:10 am

    In the blocks that look like:

    if (left) {
        if(right){
            right = false;
            x = x - 10; shot = false;
        }
    }
    

    I think you should have x = x - 10; outside the second if:

    if (left) {
        if(right){
            right = false;
            shot = false;
        }
        x = x - 10;
    }
    

    although I’m not really sure what that inner if is for, maybe you should remove it completely (but keep the x -= 10 etc.). This should be sufficient to make the movement seem natural.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.