0

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

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

面板.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;
        }
    }
}
4

1 回答 1

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

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

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

我建议您从阅读Swing 教程开始。本教程中的某些部分涵盖了上述几点。

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

  2. static final 常量变量应该完全大写,而不仅仅是第一个字母。

于 2013-04-25T02:30:21.800 回答