Fix bug, introduced new particles bug

master
John 2022-06-14 22:00:27 -04:00
parent 3c90ee6aa3
commit e4b18eaa85
2 changed files with 13 additions and 7 deletions

View File

@ -28,6 +28,7 @@ public class GameFrame extends JFrame{
try { try {
game = (GamePanel)FileManager.readObjectFromFile("local/game_state", Arrays.asList("Any")); game = (GamePanel)FileManager.readObjectFromFile("local/game_state", Arrays.asList("Any"));
game.gameFrame = main; game.gameFrame = main;
game.addUserInterface();
game.startThread(); game.startThread();
} catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) { } catch (IOException | ClassNotFoundException | ClassCastException | SecurityException e) {
System.out.println(e); System.out.println(e);

View File

@ -102,6 +102,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
} }
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, playerSpriteArray); //create a player controlled player, set start location to middle of screenk player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, playerSpriteArray); //create a player controlled player, set start location to middle of screenk
// the height of 35 is set because it is half of the original tile height (i.e., 70px) // the height of 35 is set because it is half of the original tile height (i.e., 70px)
addUserInterface();
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
}
public void addUserInterface() {
this.setFocusable(true); //make everything in this class appear on the screen this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input this.addKeyListener(this); //start listening for keyboard input
// request focus when the CardLayout selects this game // request focus when the CardLayout selects this game
@ -138,7 +143,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
player.mouseMoved(e); player.mouseMoved(e);
} }
}); });
this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
} }
// startThread is to be called after the game has started to avoid any issues TODO: better explanation // startThread is to be called after the game has started to avoid any issues TODO: better explanation
@ -146,7 +150,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time! //make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread = new Thread(this); gameThread = new Thread(this);
gameThread.start(); gameThread.start();
System.out.println(gameFrame); System.out.println(gameThread);
} }
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself //paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself
@ -197,6 +201,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
} }
} }
for(int i=0; i<particles.size(); i++){ for(int i=0; i<particles.size(); i++){
// todo: find cause of particles being null
if(i<particles.size()) { if(i<particles.size()) {
particles.get(i).draw(g); particles.get(i).draw(g);
particles.get(i).lifeSpan--; particles.get(i).lifeSpan--;
@ -329,11 +334,11 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
playerFrame = (playerFrame + 1) % 11; playerFrame = (playerFrame + 1) % 11;
playerFrameCounter -= 5; playerFrameCounter -= 5;
// if the player has moved enough to justify a frame change, a new save will also be made // if the player has moved enough to justify a frame change, a new save will also be made
// try { try {
// FileManager.writeObjectToFile("local/game_state", this); FileManager.writeObjectToFile("local/game_state", this);
// } catch (IOException e) { } catch (IOException e) {
// e.printStackTrace(); e.printStackTrace();
// } }
} }
} }
repaint(); repaint();