在Java中简单的侧滚动游戏滞后

时间:2011-12-22 18:57:58

标签: java swing timer lag side-scroller

我最近在java中构建了我的第一个简单的侧滚动游戏,但我遇到了一些滞后,我不知道为什么。当我侧身移动我的角色时,它似乎正在改变速度,大部分时间都在快速但有时会减速。它让游戏的感觉很奇怪。我希望有人可以在这里指出我正确的方向,我将在下面发布我的课程:

主类:

public class MainGame {

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

}

帧类:

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

}

董事会成员:

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

}

玩家等级:

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 个答案:

答案 0 :(得分:1)

计时器没有实时保证。因此,您应该计算上次调用移动时间的时间差,并将其乘以您移动的数量。它会使数字变小,所以我会添加另一个const来使它变大。实际上,最好在actionPerformed(或其他进程方法)中获取时间delta,并将其传递给所有进程或移动方法,以便所有内容都使用相同的时间delta进行移动。