I am aware that this has probably been posed and solved many times before however I've been unable to find any concrete help after a quick search. I am currently making a platformer game which involves a scrolling panel and a sprite. My intention is for the sprite to be able to jump. My code for the frame that the scrolling panel sits on looks like this:
package Main; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.Timer; import Graphics.AadGamePanel; import Graphics.AadJumpPanel; import Graphics.AadMenuPanel; public class MainFrame extends JFrame implements KeyListener, ActionListener { private AadGamePanel gamePanel = null; private Timer timer = null; public MainFrame() { setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setTitle("Jump"); addKeyListener(this); Container content = getContentPane(); AadMenuPanel menuPanel = new AadMenuPanel(); content.add(menuPanel); pack(); setVisible(true); while (!menuPanel.getGameStarted()) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } content.remove(menuPanel); content.revalidate(); content.repaint(); gamePanel = new AadGamePanel(content); content.revalidate(); content.repaint(); timer = new Timer(1, this); timer.start(); } public void keyPressed(final KeyEvent e) { gamePanel.keyPressed(e); } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void actionPerformed(ActionEvent arg0) { AadJumpPanel jumpPanel = gamePanel.getJumpPanel(); jumpPanel.jump(); repaint(); } }
The code for the panel itself looks like this:
package Graphics; import java.awt.Dimension; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.IOException; import java.util.ArrayList; import javax.swing.JPanel; import Main.MainFrame; public class AadJumpPanel extends JPanel implements KeyListener { private AadJumpMan jumpMan = new AadJumpMan(); private ArrayList listOfComponents = new ArrayList(); private boolean jumping = false; private int yNew = 0; public AadJumpPanel() { setLayout(null); setPreferredSize(new Dimension(800, 238)); setOpaque(false); try { initialisePanelFormat(); } catch (IOException e) { e.printStackTrace(); } } private void initialisePanelFormat() throws IOException { final AadGround ground1 = new AadGround(); final AadGround ground2 = new AadGround(); final AadHole hole1 = new AadHole(); final AadGround ground3 = new AadGround(); ground1.setBounds(0, 88, 267, 150); ground2.setBounds(267, 88, 267, 150); ground3.setBounds(801, 88, 267, 150); hole1.setBounds(534, 88, 267, 150); jumpMan.setBounds(0, 0, 267, 150); add(ground1); add(ground2); add(hole1); add(ground3); add(jumpMan); listOfComponents.add(ground1); listOfComponents.add(ground2); listOfComponents.add(hole1); listOfComponents.add(ground3); } public AadJumpMan getJumpMan() { return jumpMan; } public ArrayList getListOfComponents() { return listOfComponents; } public void jump() { int x = jumpMan.getX(); int width = jumpMan.getWidth(); int height = jumpMan.getHeight(); jumpMan.setBounds(x, yNew, width, height); } public void keyPressed(KeyEvent e) { synchronized (this) { int key = e.getKeyCode(); if (key == KeyEvent.VK_SPACE && !jumping) { jumping = true; for (int i=0; i<100; i++) { int y = jumpMan.getY(); yNew = y+4; } for (int i=0; i<100; i++) { int y = jumpMan.getY(); yNew = y-4; } jumping = false; } } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }
Currently I have the sprite (jumpMan) displayed on the JumpPanel. This is where I have a key listener that intends to control the jumping of the sprite by incrementally moving the sprite through the space to simulate jumping slightly more accurately. The JumpPanel is laid on top of a panel called the GamePanel which sits between the main frame and the JumpPanel. I currently have a timer on the main frame which is firing off the action listener every 5ms.
My hope was that the action performed would repaint at intervals so that the you would be able to see the sprite at different stages of the jump and then as it descends as well. However when I try to make the sprite jump nothing happens, nothing changes on the screen - when I debug through I can see the code going into both the keyPressed and actionPerformed methods but this doesn't seem to do anything.
Any help would be appreciated. Thanks