基于图块的游戏在我运行时经常会冻结。我怎样才能解决这个问题?

时间:2013-04-25 01:55:59

标签: java

我最近发布了这个,但我的问题措辞不正确,所以我再试一次。我一直在努力研究基于磁贴的Java游戏作为我的Java类的项目。到目前为止一切都进展顺利,但我遇到的一个非常大的问题是,当我运行游戏时,它会在大约50%的时间内开始冻结,而不是对按下的任何按键或任何类似的按键做出反应。有时它会起作用,有时则不起作用。它在结构上与this tutorial有些相似,但显然我做了很多改动和补充。我感觉它在我的游戏面板课程中正在发生,因为当游戏正确运行时,一切似乎都按预期运行,所以我会发布该类。我也为我的代码中的任何奇怪或非传统道歉,我仍然是编码的业余爱好者所以如果需要澄清,我将提供它。

所以我的主要问题是,是否有人知道问题是什么,或者对如何解决冻结有任何建议?谢谢!

panel.java:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class panel extends JPanel implements KeyListener, Runnable
{
    //game variables
    private Thread game;
    private boolean running = false;
    private boolean mapFinished = false;
    player p1;

    //panel variables
    static final int Width = 480;
    static final int Height = 432;
    static final Dimension dim = new Dimension(Width,Height);

    //maps
    map map1;
    map map2;
    map map3;
    map map4;

    enemy e;

    boolean map1Finished;
    boolean map2Finished;
    boolean map3Finished;
    boolean map4Finished;

    //drawing variables
    private BufferedImage image;
    private Graphics g;
    private long time = 6*1000000;
    goal ng;
    private Image winningImage;

    private boolean map2Spawn;
    private boolean map3Spawn;
    private boolean map4Spawn;

    public panel(){
        map1 = new map("tester.txt");
        map2 = new map("tester2.txt");
        map3 = new map("tester3.txt");
        map4 = new map("tester4.txt");
        p1 = new player(map1);
        ng = new goal(map1);
        e = new enemy(map1);
        setPreferredSize(new Dimension(Width,Height));
        setFocusable(true);
        requestFocus();
    }

    public void run(){
        long startTime;
        long elapsedTime;
        long diff;
        long sleepTime;
        long overSleep=0;

        image=new BufferedImage(Width,Height,BufferedImage.TYPE_INT_RGB);
        g=image.getGraphics();
        running = true;

        while (running){
            startTime=System.nanoTime();
            gameUpdate();
            gameRender();
            gameDraw();
            elapsedTime=System.nanoTime();
            diff=elapsedTime-startTime;
            if(diff<time){
                sleepTime=time-diff-overSleep;
                try{
                    game.sleep(sleepTime/1000000);
                }
                catch(Exception e){

                }
            }
            else{
                overSleep=diff-time;
            }
        }
    }

    private void gameUpdate(){
        p1.update();

        if((p1.playerRec.x/48)==(ng.goalRec.x/48) && (p1.playerRec.y/48)==(ng.goalRec.y/48)){
            if(!map1Finished){
                map1Finished=true;
            }
            else if(map1Finished&&!map2Finished){
                map2Finished=true;
            }
            else if(map2Finished&&!map3Finished){
                map3Finished=true;
            }
            else if(map3Finished&&!map4Finished){
                map4Finished=true;
            }

        }

        changeSpawn();
    }

    private void gameRender(){
        g.setColor(Color.WHITE);
        g.fillRect(0,0,Width,Height);
        g.setColor(Color.BLACK);
        if (!map1Finished){
            map1.draw(g);
            p1.draw(g);
        }


        if (map1Finished&&!map2Finished){
            map2.draw(g);
            p1.draw(g);
            winningImage=new ImageIcon("sprites/level1.png").getImage();
            g.drawImage(winningImage,5,5,null);
        }
        if (map2Finished&&!map3Finished){
            map3.draw(g);
            p1.draw(g);
            winningImage=new ImageIcon("sprites/level1.png").getImage();
            g.drawImage(winningImage,5,5,null);
        }
        if (map3Finished&&!map4Finished){
            map4.draw(g);
            p1.draw(g);
            winningImage=new ImageIcon("sprites/level1.png").getImage();
            g.drawImage(winningImage,5,5,null);
        }
        //g.drawString(""+map1.tileMap[1][7], 100, 100);
    }

    public void gameDraw(){
        Graphics g2=this.getGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
    }

    public void keyTyped(KeyEvent key) {}

    public void keyPressed(KeyEvent key) {

    int code = key.getKeyCode();    
    if(code == KeyEvent.VK_LEFT) {
            p1.setLeft(true);
    }
    if(code == KeyEvent.VK_RIGHT) {
            p1.setRight(true);
    }
    if(code == KeyEvent.VK_UP) {
            p1.setUp(true);
    }
    if(code == KeyEvent.VK_DOWN) {
            p1.setDown(true);
    }
    }

    public void keyReleased(KeyEvent key) {
    int code = key.getKeyCode();
    if(code == KeyEvent.VK_LEFT) {
        p1.setLeft(false);
    }
    if(code == KeyEvent.VK_RIGHT) {
        p1.setRight(false);
    }
    }

    public void addNotify(){
        super.addNotify();
        if(game==null){
            game=new Thread(this);
            game.start();
        }
        addKeyListener(this);
    }

    public void startGame(){
        if (running == false){
            running = true; 
        }
    }

    public void stopGame(){
        if (running == true)
        {
            running = false; 
        }
    }

    public boolean boolTimer(){
        long now=System.currentTimeMillis();
        long end = now+1*3000;
        long current = System.currentTimeMillis();
        while(current<end){
            current = System.currentTimeMillis();
        }
        return true;
    }

    public void changeSpawn(){
        if(map1Finished==true && map2Spawn==false){
            p1=new player(map2);
            ng=new goal(map2);
            map2Spawn=true;
        }

        else if(map2Finished==true && map3Spawn==false){
            p1=new player(map3);
            ng=new goal(map3);
            map3Spawn=true;
        }
        else if(map3Finished==true && map4Spawn==false){
            p1=new player(map4);
            ng=new goal(map4);
            map4Spawn=true;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  1. 不要使用Swing组件的getGraphics()来绘画。自定义绘画是通过覆盖JPanel的paintComponent()方法完成的。

  2. 使用Swing Timer来安排动画,而不是线程。

  3. 不要使用KeyListener来侦听KeyEvents。相反,您应该使用Key Bindings

  4. 我建议你先阅读Swing tutorial。本教程中有一些部分涵盖了以上几点。

    1. 请勿使用requestFocus()。更好的使用方法是requestFocusInWindow()。虽然除非组件显示在可见的GUI上,否则这两种方法都不会起作用,因此将代码放在构造函数中不起作用。

    2. 静态最终常量变量应该完全是上限,而不仅仅是第一个字母。