catsaremyreligion
Senior Newbie 
|
 |
«
Posted
2013-04-26 17: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 { private boolean running = false; private boolean mapFinished = false; player p1; static final int Width = 480; static final int Height = 432; static final Dimension dim = new Dimension(Width,Height); map map1; map map2; map map3; map map4; map end; enemy e; boolean map1Finished; boolean map2Finished; boolean map3Finished; boolean map4Finished; 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); } 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); } } } 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; } } } } } |
|
|
|
|
catsaremyreligion
Senior Newbie 
|
 |
«
Reply #1 - Posted
2013-04-27 00:30:11 » |
|
Can no one help me with this???
|
|
|
|
masteryoom
|
 |
«
Reply #2 - Posted
2013-04-27 00: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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
masteryoom
|
 |
«
Reply #4 - Posted
2013-04-27 01: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  .
|
|
|
|
catsaremyreligion
Senior Newbie 
|
 |
«
Reply #5 - Posted
2013-04-27 01:09:16 » |
|
I mean, I don't press anything until it's fully loaded though. 
|
|
|
|
JESTERRRRRR
|
 |
«
Reply #6 - Posted
2013-04-27 01:52:35 » |
|
If you post the other classes I'll give it a try and see if I can find the problem
|
|
|
|
|
catsaremyreligion
Senior Newbie 
|
 |
«
Reply #8 - Posted
2013-04-27 03:05:15 » |
|
Did that help at all?
|
|
|
|
JESTERRRRRR
|
 |
«
Reply #9 - Posted
2013-04-28 15: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!
|
|
Jimmt
|
 |
«
Reply #10 - Posted
2013-04-29 02: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? 
|
|
|
|
Kerai
|
 |
«
Reply #11 - Posted
2013-04-29 11:58:48 » |
|
Can you pack your whole project into zip? I will then run it and try debug your problem.
|
|
|
|
davidc
|
 |
«
Reply #12 - Posted
2013-04-29 13: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
|
|
|
|
JESTERRRRRR
|
 |
«
Reply #13 - Posted
2013-04-30 01: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
|
|
|
|
davidc
|
 |
«
Reply #14 - Posted
2013-04-30 01: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.
|
|
|
|
Jimmt
|
 |
«
Reply #15 - Posted
2013-04-30 04: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".
|
|
|
|
HeroesGraveDev
|
 |
«
Reply #16 - Posted
2013-04-30 08: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.
|
|
|
|
Jimmt
|
 |
«
Reply #17 - Posted
2013-04-30 16: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  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.
|
|
|
|
|