Christian.H
Christian.H

Reputation: 148

Lag in simple side scrolling game in Java

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;
            }
        }

}

Upvotes: 0

Views: 1645

Answers (1)

mholzmann
mholzmann

Reputation: 1314

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.

Upvotes: 1

Related Questions