Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Making a sprite jump

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Making a sprite jump

    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


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Making a sprite jump

    There are parts you're not showing us, but I suspect your animation technique is not correct. Refer to the Custom Painting tutorial and ensure that your general approach is:

    A main container (JFrame) that contains
    A JPanel component on which the graphics are drawn
    A javax.swing.Timer that varies the graphics parameters and repaints the JPanel

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a sprite jump

    Ok I've read the tutorial I still don't understand why the painting isn't working.
    The problem seems to be due to the code being executed in the keyPressed method? I tried taking the code out of here and putting it elsewhere and the sprite then repainted succesfully.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,718
    My Mood
    Mellow
    Thanks
    231
    Thanked 706 Times in 692 Posts

    Default Re: Making a sprite jump

    When you moved the code, where did you put it?

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    16
    My Mood
    Tolerant
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Making a sprite jump

    I moved it to a method that already controlled the automatically scrolling background which I have already managed to get working. I just wanted to see if I could get the sprite to move and up and down albeit without pressing any buttons and this did work

Similar Threads

  1. sprite
    By game06 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 14th, 2013, 09:06 PM
  2. Replies: 9
    Last Post: February 24th, 2013, 05:51 PM
  3. [SOLVED] need help with basic jump command
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 23rd, 2012, 01:34 AM
  4. jump use
    By TarunN in forum Java Theory & Questions
    Replies: 6
    Last Post: April 30th, 2010, 04:03 AM
  5. How To Make The Program Jump To The Next Function..
    By Kumarrrr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2010, 11:33 PM