My game project freezing when I run it?

Hey guys, I’ve posted this question on a number of different places, but no one could help me. Someone finally recommended that I try here.

I’ve been diligently working on a 2-D tile-based Java game as a project for my Java class, and it’s been a lot of fun. Everything is going very well so far, but a pretty big problem that I’m having is that when I run my game, it will start frozen about 50% of the time, as the player won’t react to any keys pressed or anything like it should. Sometimes it works and sometimes it doesn’t. I have a feeling it is happening in my game panel class, because everything seems to function as intended when the game DOES run correctly, so I’ll post that class. I also apologize for anything strange or unconventional in my code, I’m still kind of an amateur at coding so if clarification is needed I will supply it.

tl;dr: Why does my game freeze when I run it sometimes?

here’s my panel class:

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

public class panel extends JPanel implements KeyListener, ActionListener
{
    //game variables
    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;
    map end;
    
    enemy e;
    
    boolean map1Finished;
    boolean map2Finished;
    boolean map3Finished;
    boolean map4Finished;
    
    //drawing variables
    private BufferedImage image;
    private Graphics g;
    goal ng;
    private Image levelImage;
    
    private boolean map2Spawn;
    private boolean map3Spawn;
    private boolean map4Spawn;
    fireball fb;
    private boolean fireExist=false;
    private boolean gameOver=false;
    Timer loopTimer;
    
    public panel(){
        map1 = new map("tester.txt");
        map2 = new map("tester2.txt");
        map3 = new map("tester3.txt");
        map4 = new map("tester4.txt");
        end = new map("gameover.txt");
        p1 = new player(map1);
        ng = new goal(map1);
        e = new enemy(map1);
        setPreferredSize(new Dimension(Width,Height));
        setFocusable(true);
        requestFocus();
        fb=null;
        this.addKeyListener(this);
	loopTimer = new Timer(10, this);
	loopTimer.start();
    }
    
    @Override
    public void actionPerformed(ActionEvent evt){
	if (!running){
		startGame();
	}
	gameUpdate();
	repaint();
}
    
    private void gameUpdate(){
        if(gameOver==true){
            running=false;
        }
        p1.update();
        e.getPlayerCoord(p1.playerRec.x,p1.playerRec.y);
        e.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;
            }  
        }
        if(e.fireShot==true&&fireExist==false){
            fb=new fireball(e.fireX,e.fireY,e.fireDir);
            fireExist=true;
        }
        if(fireExist==true){
            if(fb.playerCol(p1.playerRec.x,p1.playerRec.y)==true){
                gameOver=true;
            }
        }
        fireWallCol();
        changeSpawn();
        
    }
    
    @Override
    public void paintComponent(Graphics g){
	super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0,0,Width,Height);
        if(gameOver==true){
            end.draw(g);
            levelImage=new ImageIcon("sprites/gameover.png").getImage();
            g.drawImage(levelImage,100,100,null);
            //running=false;
        }
        if(gameOver==false){
            if (!map1Finished){
                map1.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
            }
        
            if (map1Finished&&!map2Finished){
                map2.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
                levelImage=new ImageIcon("sprites/level1.png").getImage();
                g.drawImage(levelImage,5,5,null);
            }
            if (map2Finished&&!map3Finished){
                map3.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
                levelImage=new ImageIcon("sprites/level2.png").getImage();
                g.drawImage(levelImage,5,5,null);
            }
            if (map3Finished&&!map4Finished){
                map4.draw(g);
                p1.draw(g);
                e.draw(g);
                if (fireExist==true){
                    fb.draw(g);
                }
                levelImage=new ImageIcon("sprites/level3.png").getImage();
                g.drawImage(levelImage,5,5,null);
            }
        }
        //g.drawString(""+map1.tileMap[1][7], 100, 100);
    }
    
    
    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) {
    }
    
    
    public void startGame(){
        if (running == false){
            running = true; 
        }
    }
    
    public void stopGame(){
        if (running == true)
        {
            running = false; 
        }
    }
    
    public void changeSpawn(){
        if(map1Finished==true && map2Spawn==false){
            p1=new player(map2);
            ng=new goal(map2);
            e=new enemy(map2);
            if(fireExist==true){
                fireExist=false;
            }
            map2Spawn=true;
        }
        
        else if(map2Finished==true && map3Spawn==false){
            p1=new player(map3);
            ng=new goal(map3);
            e=new enemy(map3);
            if(fireExist==true){
                fireExist=false;
            }
            map3Spawn=true;
        }
        else if(map3Finished==true && map4Spawn==false){
            p1=new player(map4);
            ng=new goal(map4);
            e=new enemy(map4);
            if(fireExist==true){
                fireExist=false;
            }
            map4Spawn=true;
        }
    }
    public void fireWallCol(){
        if(fireExist==true){
            if(!map1Finished){
                    if(map1.tileMap[fb.fireRec.y/48][fb.fireRec.x/48]==1){
                        fb=null;
                        fireExist=false;
                        e.fireShot=false;
                    }
            }
            if(map1Finished&&!map2Finished){
                    if(map2.tileMap[fb.fireRec.y/48][fb.fireRec.x/48]==1){
                        fb=null;
                        fireExist=false;
                        e.fireShot=false;
                    }
            }
            if(map2Finished&&!map3Finished){
                    if(map2.tileMap[fb.fireRec.y/48][fb.fireRec.x/48]==1){
                        fb=null;
                        fireExist=false;
                        e.fireShot=false;
                    }
            }
        }    
    }
}

Can no one help me with this???

Could you post your player class please? And also, if you have code like that, please put it in pastebin.
EDIT: It could possibly be that you have no code in your keyReleased function.

Whoops sorry. Here’s the pastebin to my player class:

http://pastebin.com/8NhSXwLj

I think it might be because you had pressed a button while the game was loading so that it would not take any input. I might be wrong though ;).

I mean, I don’t press anything until it’s fully loaded though. :clue:

If you post the other classes I’ll give it a try and see if I can find the problem

enemy: http://pastebin.com/5PadEpTv
map: http://pastebin.com/jcJAyian
fireball: http://pastebin.com/w31QSJUJ
goal: http://pastebin.com/ectSyUfP

There’s the rest! Thanks a lot!

Did that help at all?

Hey, nothing jumps out at me and with those files its running, if you can post everything I need to run it exactly as you are (main method and a map file or w/e) then I’ll be happy to find out whats wrong

I don’t really feel like reading through an entire program but you should probably follow code convention; class names should have their first name capitalized, final variables should be all caps with underscores as spaces, etc.
Shouldn’t they have taught you this in java class? :clue:

Can you pack your whole project into zip? I will then run it and try debug your problem.

As well as fixing up your naming conventions, you really need to learn how to use the throws clause. Your map constructor is trapping any exceptions that might be useful and discarding them. This is the worst possible thing you can do. I’ve lost count of how many times I’ve told people this, but never catch an exception unless you’re planning on actually handling it. In other words, only catch it when you can handle it appropriately. Doing nothing is not exception handling. Neither is just writing to System.out and then carrying on. /rant

I like to catch my exceptions so my program does not crash, and System.out lets me know what happened, but I dont want to jack your thread over it, do what you want. Like Kerai said, if you pack the whole thing up I will also happily check it

In the case of the map class above, it makes absolutely no sense. Unless the only exceptions that can be raised in that block can be recovered from, there is no point continuing. throws will stop it from trying to carry on (potentially causing more errors to confuse the situation) and if the exception is allowed to be propagated from main a stack trace will be printed anyway. Fewer lines of code for a better result.

I know this might seem like a bit of a thread highjack, but the practice of writing empty catch blocks is an extraordinarily bad one and should be stamped out wherever it is seen.

I wouldn’t put it at extraordinarily bad, but it definitely isn’t good practice in most cases. Exceptions happen for a reason, and you should deal with them properly - probably the simplest example is dividing by 0 in a calculator app, just popup text that says “can’t divide by 0”.

Worst. Response. Ever.

All that could possibly do is result in a missed keydown event.

If doing that gives that result for you, you have some seriously messed-up code.

Didn’t exactly get that response either :stuck_out_tongue:
Read through your first code file, found some more bad practices that may/may not be related to your freezing problem. You have a timer updating every 0.01 seconds supposedly, but timer is very inaccurate. Instead of a real loop you are just calling repaint() and putting your code into paintComponent(). And, try using an ArrayList for your maps.

Didn’t exactly get that response either :stuck_out_tongue:
Read through your first code file, found some more bad practices that may/may not be related to your freezing problem. You have a timer updating every 0.01 seconds supposedly, but timer is very inaccurate. Instead of a real loop you are just calling repaint() and putting your code into paintComponent(). And, try using an ArrayList for your maps.