Question

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.

Was it helpful?

Solution

  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.

OTHER TIPS

Try calling

repaint();

to refresh the display of the JFrame

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top