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