简单的2D java游戏滞后于mac

时间:2016-12-10 22:29:12

标签: java macos 2d lag

在过去一周半的时间里,我创建了一个简单的自上而下的2D Java游戏。它在我的Windows计算机上运行得很好,我的一些朋友也在他们的计算机上运行得很好。

最近,我尝试将游戏加载到我的Mac上,但它似乎滞后可怕并且减慢了整台计算机的速度。这仅在游戏运行时发生,并在退出时恢复正常。

我已经在下面添加了一个可能导致问题的课程,但我相信它很可能与我的电脑有关。

public class Game implements Runnable {
//DISPLAY
private Display display;
private int width, height;
public String title;
//THREADS
private boolean running = false;
private Thread thread;
//BUFFERING AND HRAPHICS
private BufferStrategy bs;
private Graphics g;

// Statess
public States gameState, island;
private States menuState;

// Input
private KeyManager keyManager;
private MouseManager mouseManager;
//UI

// Handler
private Handler handler;
//TIMER
Timer myTimer = new Timer();
public static int seconds = 0;
int count = 0;
//PAUSE
private boolean paused = false;
public Game(String title, int width, int height) {
    this.width = width;
    this.height = height;
    this.title = title;
    keyManager = new KeyManager();
    mouseManager = new MouseManager();


}

private void init() {
    display = new Display(title, width, height);
    display.getFrame().addKeyListener(keyManager);
    display.getFrame().addMouseListener(mouseManager);
    display.getFrame().addMouseMotionListener(mouseManager);
    display.getCanvas().addMouseListener(mouseManager);
    display.getCanvas().addMouseMotionListener(mouseManager);
    Assets.init();

    handler = new Handler(this);

    gameState = new GameState(handler, 9, 79, 1502, 79);
    island = new Island(handler, 20, 190, 1450, 190);
    menuState = new MenuState(handler);
    States.setState(menuState);

}



private void tick() {
    keyManager.tick();
    setPaused();
    if(!paused){
        States.getState().tick();
        if(count == 0){
        if(States.getState() != menuState){
            myTimer.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    seconds++;
                }
            }, 1000, 1000);
        count = 1;  
        }
        }
        //System.out.println(seconds);
    }
    render();
}


public void paused(Graphics g){
    g.drawString("Paused", 750, 200);
}

private void render() {

    bs = display.getCanvas().getBufferStrategy();
    if (bs == null) {
        display.getCanvas().createBufferStrategy(3);
        return;
    }
    g = bs.getDrawGraphics();
    // Clear Screen
    g.clearRect(0, 0, width, height);
    // Draw Here!
    //Rendering
    //State render

        States.getState().render(g);
        //UI
        if(States.getState() != menuState){
            if(paused)
            paused(g);
        }
    // End Drawing!
    bs.show();
    g.dispose();
}



public void run() {

    init();

    int fps = 60;
    double timePerTick = 1000000000 / fps;
    double delta = 0;
    long now;
    long lastTime = System.nanoTime();
    long timer = 0;
    //int ticks = 0;

    while (running) {
        now = System.nanoTime();
        delta += (now - lastTime) / timePerTick;
        timer += now - lastTime;
        lastTime = now;

        if (delta >= 1) {
            tick();

            //ticks++;
            delta--;
        }

        if (timer >= 1000000000) {

            //ticks = 0;
            timer = 0;
        }
    }

    stop();

}

public States getGameState(){
    return gameState;
}
public KeyManager getKeyManager() {
    return keyManager;
}

public MouseManager getMouseManager() {
    return mouseManager;
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}
//TIMER


public synchronized void start() {
    if (running)
        return;
    running = true;
    thread = new Thread(this);
    thread.start();
}

public synchronized void stop() {
    if (!running)
        return;
    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public boolean isPaused() {
    return paused;
}

public void setPaused() {
    if(handler.getKeyManager().keyJustPressed(KeyEvent.VK_U))
         paused = !paused;
}
}

Display.java

public class Display {

    private String title;
    private int height;
    private int width;
    private Canvas canvas;
    private JFrame frame;

    public Display(String title, int width, int height) {
        this.title = title;
        this.height = height;
        this.width = width;
        createDisplay();
    }

    public void createDisplay() {
        // Make frame
        frame = new JFrame(title);
        frame.setSize(width, height);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        // canvas to draw graphics
        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(width, height));
        canvas.setMaximumSize(new Dimension(width, height));
        canvas.setMinimumSize(new Dimension(width, height));
        canvas.setFocusable(false);
        // Display canvas through jframe
        frame.add(canvas);
        frame.pack();
    }
    public Canvas getCanvas(){
        return canvas;
    }
    public JFrame getFrame(){
        return frame;
    }
}

1 个答案:

答案 0 :(得分:0)

这是与您的操作系统相关的问题,请根据已经提出的问题改进您的代码:

Link 1

Link 2

还尝试更新Java版本,如果可能,请缩短代码。