Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  My game project freezing when I run it?  (Read 960 times)
0 Members and 1 Guest are viewing this topic.
Offline catsaremyreligion

Senior Newbie





« Posted 2013-04-26 19:43:36 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
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;
                    }
            }
        }    
    }
}
Offline catsaremyreligion

Senior Newbie





« Reply #1 - Posted 2013-04-27 02:30:11 »

Can no one help me with this???
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #2 - Posted 2013-04-27 02:50:17 »

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.

Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline catsaremyreligion

Senior Newbie





« Reply #3 - Posted 2013-04-27 02:58:12 »

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

http://pastebin.com/8NhSXwLj

Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #4 - Posted 2013-04-27 03:01:37 »

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  Wink.

Smiley
Offline catsaremyreligion

Senior Newbie





« Reply #5 - Posted 2013-04-27 03:09:16 »

I mean, I don't press anything until it's fully loaded though.  Clueless
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #6 - Posted 2013-04-27 03:52:35 »

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

Senior Newbie





« Reply #7 - Posted 2013-04-27 03:58:05 »

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!
Offline catsaremyreligion

Senior Newbie





« Reply #8 - Posted 2013-04-27 05:05:15 »

Did that help at all?
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #9 - Posted 2013-04-28 17:36:32 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #10 - Posted 2013-04-29 04:46:39 »

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? Clueless
Offline Kerai

Junior Member


Medals: 4



« Reply #11 - Posted 2013-04-29 13:58:48 »

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

Senior Member


Medals: 5
Projects: 2



« Reply #12 - Posted 2013-04-29 15:03:42 »

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
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #13 - Posted 2013-04-30 03:25:37 »

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
Offline davidc

Senior Member


Medals: 5
Projects: 2



« Reply #14 - Posted 2013-04-30 03:59:19 »

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.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #15 - Posted 2013-04-30 06:13:37 »

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".
Offline HeroesGraveDev

JGO Kernel


Medals: 239
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #16 - Posted 2013-04-30 10:55:55 »

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

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.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #17 - Posted 2013-04-30 18:50:22 »

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

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 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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!