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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:44:17+00:00 2026-05-27T16:44:17+00:00

I recently built my first simple side scrolling game in java, but I’m experiencing

  • 0

I recently built my first simple side scrolling game in java, but I’m experiencing some lag and I don’t know why. When I move my character sideways it seems to be changing speed, most of the time going fast but sometimes slowing down. It makes the feel of the game very odd. I hope someone can point me in the right direction here, I’ll post my classes below:

Main Class:

public class MainGame {

    public static void main(String[] args) {
        Frame frame = new Frame();
    }

}

Frame Class:

import javax.swing.JFrame;


public class Frame extends JFrame{

    public Frame(){
        add(new Board());
        setTitle("2-D Test Game");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800,325);
        this.setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

}

Board Class:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;


public class Board extends JPanel implements ActionListener{


    private Player p;
    private Timer timer;
    private Image background;

    public Board(){
        super();
        p = new Player();
        addKeyListener(new ActionListener());
        setFocusable(true);
        ImageIcon i = new ImageIcon("C:/test.png");
        background = i.getImage();
        timer = new Timer(5,this);
        timer.start();
    }

    public void actionPerformed(ActionEvent arg0) {
        p.move();
        repaint();
    }

    public void paint(Graphics g){
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(background,p.nx,0,null);
        g2d.drawImage(p.getImage(), 350, p.getY(), null);
    }

    private class ActionListener extends KeyAdapter{
        public void keyReleased(KeyEvent e){
            p.keyReleased(e);
        }

        public void keyPressed(KeyEvent e){
            p.keyPressed(e);
        }
    }

}

Player Class:

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


public class Player {

    private Image img;
    int x,y,dx,dy,nx;
    long time;
    private final int SPEED = 2;
    private final int GRAV = 1;
    boolean left,right,isJumping;

    public Player(){
        ImageIcon i = new ImageIcon("C:/plager.png");
        img = i.getImage();
        x = 350;
        y = 160;
        dx = 0;
        dy = 0;
        nx = 0;
        left = false;
        right = false;
        isJumping = false;
    }

    public void move(){
        x += dx;
        nx = (nx-dx);
    }


    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    public Image getImage(){
        return img;
    }

        public void keyReleased(KeyEvent e) {
            int code = e.getKeyCode();
            if(code == KeyEvent.VK_LEFT){
                left = false;
                if(right){
                    dx = SPEED;
                }else{
                    dx = 0;
                }
            }else if(code == KeyEvent.VK_RIGHT){
                right = false;
                if(left){
                    dx = -SPEED;
                }else{
                    dx = 0;
                }
            }
        }

        public void keyPressed(KeyEvent e) {
            int code = e.getKeyCode();
            if(code == KeyEvent.VK_LEFT){
                left = true;
                dx = -SPEED;
            }else if(code == KeyEvent.VK_RIGHT){
                right = true;
                dx = SPEED;
            }
        }

}
  • 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-05-27T16:44:18+00:00Added an answer on May 27, 2026 at 4:44 pm

    Timer does not have a real time guarantee. So, you should calculate the time difference from the last time move was called and multiply that by the amount you are moving. It will make the numbers smaller so I would add another const to make it larger. It’s actually better get the time delta in actionPerformed (or another process method) and pass it to all of the process or move methods so everything moves using the same time delta.

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

Sidebar

Related Questions

Recently, I've been involved in two projects. The first one is built on php
I'm using Xode 4, and i have recently uploaded my first app... but now
I recently came across some odd behavior that involves Java's regex engine. When writing
I recently built an API and was often asked to replace the StringCollections I
I recently built a program that parses a remote file from \some_server\c$\directory\file.xls and it
We recently built a web app using Prototype, making a fair amount of use
We recently built a large ASP.NET web forms application for a client and the
We recently built a new dev server for flex builder and installed coldfusion 8.
Recently I upgraded to Snow Leopard, and now I can't run programs built with
I've recently been playing with nodejs and would like to build my first project

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.