0

I am trying to make a platformer game for my computer studies class, and right now i'm just getting the movement of the character down, aka the jumping and the moving from side to side. I got the jump down, and it works fine when called from the constructor, however, when it is called from an Event Listener, the frame doesn't update, and the character just jumps from one place to another without any kind of animation. I have no idea why this is happening, any help would be greatly appreciated, and if you've got any advice for making these kinds of games I would be more than happy to receive it.

Thanks in advance.

import java.awt.event.*;
import javax.swing.*;

public class LooperGui extends JFrame implements ActionListener{

//setting up all of the variables and components of the JFrame
private JLabel stick = new JLabel();
JButton g = new JButton("jump");
ImageIcon h = new ImageIcon("src//stickGuy.jpg");

int x = 100, y = 120, maxY = y, minY = 168;
double time = 5;
int fps = 25, frames = (int) (time*fps); 
double timePerFrame = (time/frames);

public LooperGui(){

    setSize(500, 500);
    //setUndecorated(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setLayout(null);
    setResizable(false);
    stick.setIcon(h);
    g.setBounds(10, 10, 100, 30);
    g.addActionListener(this);
    add(g);
    stick.setBounds(x, y, h.getIconWidth(), h.getIconHeight());
    add(stick);
    jump();//call jump from the constructor and it will be perfectly animated, the exact way that I intended it to be
}

public void jump(){

    //I attempted to make the jump as close to reality as possible so I used 
    //kinematic equations to set the characters height in the air at any given time
    //from here it is easy to change the characters side to side movement, as it is simply changing the x value

    //the first for loop if for the ascent, and the second one is for the descent
    for(double t = time; t>0; t-=timePerFrame){
        y = (int) ((9.81*(t*t))/2);
        stick.setBounds(x, y, h.getIconWidth(), h.getIconHeight());
        x+=1;
        //there may be a problem with using thread.sleep(), not really sure
        try {
            Thread.sleep(4);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    for(double t = 0; t<time; t+=timePerFrame){
        y = (int) ((9.81*(t*t))/2);
        stick.setBounds(x, y, h.getIconWidth(), h.getIconHeight());
        x+=1;
        try {
            Thread.sleep(4);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }   
}

public void actionPerformed(ActionEvent e) {
    if(e.getSource() == g){
        jump();//calling jump from the action performed method makes the character jump positions
    }

}

}

I am currently using a stickman for the character, can't link it because I don't have a high enough rep. But its pretty easy to make a shitty looking one in photoshop or paint.

2 Answers2

1
  1. Never to implement ActionListener to any component which doesn't have such listener
  2. To register listener either use inline approach using the means of anonymous class or implement to a new named class reflecting the listener and instantiate it to assign. Check out the tutorial page.
  3. Never to use null layout and setting size hints with setBounds(x, y, width, height). Learn proper layout managers.
  4. Most of the time, not to set size to JFrame. instead after layouting your component and adding them to content pane invoke pack() on it.
  5. Always put GUI rendering task inside EDT using SwingUtilities.invokeLater(Runnable).
  6. Never to to call Thread.sleep(milSecond) inside Swing GUI or event task, so it might have the chance to block EDT.
Community
  • 1
  • 1
Sage
  • 15,290
  • 3
  • 33
  • 38
  • so I should implement a keyListener to 'stick'? With regards to setting the size of the JFrame and setting the layout to null, thats the way I was taught to set up the JFrame. But I will look into the links you provided. Also the last link, the EDT made no sense to me, just started programming 4 months ago, and haven't really got into threading yet. So a more noob friendly explinatino would be great. Thanks again. – user3093022 Dec 11 '13 at 22:42
  • @user3093022, if it doesn't make sense, it is ok. You can learn about it later. But at first start learning Layout mangers. Then move to using listeners. But do not try to achieve animation at this point. Go forth one by one. Before doing gui rendering task in timely basis, you should learn Threading and Swing threading model. – Sage Dec 11 '13 at 22:45
  • Yeah a little problem with that, i'm doing this for my final project for the course, and I probably have a little over four weeks to finish programming this. However, thanks for this, and I'll be sure to try and learn all of this. – user3093022 Dec 11 '13 at 22:50
  • *Four weeks* is lots of time. Learn at least about `FlowLayout` and `BorderLayout`. Start from [How to Make frame](http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html). You won't regret it – Sage Dec 11 '13 at 22:52
  • Now, a month later, I must really thank you. My program is working wonderfully, and It is all thanks to you! Your awesome man!!! Keep up the good work! Thanks again. – user3093022 Jan 14 '14 at 02:37
  • @user3093022, I am happy to hear that you are doing great. Go forth and produce great software :) – Sage Jan 14 '14 at 13:13
0

Try calling

repaint();

to refresh the display of the JFrame