First of all, sorry for my english!
I have a problem, I´m trying to make a simple java videogame, and I make booleans for the inputs. The inputs works well, but when I try to update the booleans in another class, it doesnt work.
I put here the important classes
I put all the classes in this drive. Classes
package Launcher; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class KeyHandler implements KeyListener { public boolean upPressed, downPressed, leftPressed, rightPressed; @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { int code = e.getKeyCode(); if(code == KeyEvent.VK_W){ upPressed = true; System.out.println(upPressed); } if(code == KeyEvent.VK_S){ downPressed = false; } if(code == KeyEvent.VK_A){ leftPressed = true; } if(code == KeyEvent.VK_D){ rightPressed = true; } } @Override public void keyReleased(KeyEvent e) { int code = e.getKeyCode(); if(code == KeyEvent.VK_W){ upPressed = false; } if(code == KeyEvent.VK_S){ downPressed = false; } if(code == KeyEvent.VK_A){ leftPressed = false; } if(code == KeyEvent.VK_D){ rightPressed = false; } } }
package Launcher; import java.awt.*; import java.awt.event.KeyListener; import javax.swing.JPanel; public class GamePanel extends JPanel implements Runnable{ //Window settings variables KeyHandler KH = new KeyHandler(); final int originalTileSize = 16; //16x16px final int scale = 3; final int tileSize = originalTileSize * scale; //48x48px final int maxScreenCol = 16; //Tiles in a column final int maxScreenRow = 12; //Tiles in a row final int maxScreenWidth = tileSize * maxScreenCol; final int maxScreenHeight = tileSize * maxScreenRow; Thread gameThread; //Player pos int playerX = 100; int playerY = 100; int playerSpeed = 4; public GamePanel(){ //Window Settings with variable implementation this.setPreferredSize(new Dimension(maxScreenWidth, maxScreenHeight)); this.setBackground(Color.black); this.setDoubleBuffered(true); this.addKeyListener(KH); this.setFocusable(true); } public void startGameThread(){ gameThread = new Thread(this); gameThread.start(); } public void update(){ //System.out.println(playerY); boolean upP = KH.upPressed; //System.out.println(upP); if(upP){ System.out.println("Updating"); playerY -= playerSpeed; } else if(KH.downPressed == true){ playerY += playerSpeed; } else if(KH.leftPressed == true){ playerX -= playerSpeed; } else if(KH.rightPressed){ playerX += playerSpeed; } } public void run(){ while(gameThread != null){ //System.out.println("Running game"); //1.Update update(); //2Draw screen info repaint(); } }
I only want the update function get the boolean variables, for me to be able to move my character. Thank you so much!
Advertisement
Answer
I had a look at the other classes and the reason is outside the code above, it’s in class Launcher.Main:
public static void main(String[] args){ WindowPanel WP = new WindowPanel(); WP.windowPanel(); GamePanel GP = new GamePanel(); GP.startGameThread(); }
in here you create a new instance of class WindowPanel, which – by itself – create a another instance of GamePanel.
public void windowPanel(){ // ... GamePanel gamePanel = new GamePanel(); window.add(gamePanel); window.pack(); // ... }
So, the GamePanel that you created in Main, after you created the WindowPanel is not related to the GamePanel you create afterwards and start threading.
Fix:
- remove the additional GamePanel from Main class
- add to the end of windowPanel() method in WindowPanel class:
gamePanel.startGameThread();