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.