I'm having a bit of an issue with getting my game characters to jump. I've set my jump function as the space key, but the jump animation doesn't happen all at once. Instead, pressing space causes the character to move a little up every time until it reaches the maximum jump height limit. Then, once it reaches that height, pressing space causes the player to move a little down every time until it reaches the ground limit. It looks normal when the space button is being held down, but once it is released, the character is stuck in the air where the space bar was released. I want the sequence to happen all at once with one click of the space bar. I've looked at my code for quite some time and tried changing where I call the "act" method I have for my Player class from the JPanel. Here is my Player class where the jump feature is meant to be assigned its values:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Player {
ImageIcon movements[];
ImageIcon leftMove[];
ImageIcon move[];
ImageIcon attack;
boolean ableAttack, canJump;
int count, x, y, width, height;
int fallingSpeed = 0;
int gravity = 1;
int jumpPower = -20;
public Player(int count, int x, int y) {
this.x = x;
this.y = y;
this.count = count;
movements = new ImageIcon[8];
leftMove = new ImageIcon[8];
attack = new ImageIcon("attackRight.gif");
this.width = 80;
this.height = 130;
for (int i = 0; i < 8; i++) {
movements[i] = new ImageIcon("right_"+(i + 1) + ".png");
leftMove[i] = new ImageIcon("go_"+(i+1)+".png");
}
move = movements.clone();
}
public void act() {
if (isOnGround()) {
canJump = true;
jump();
} else {
canJump = false;
fall();
}
}
public void jump() {
fallingSpeed = jumpPower;
fall();
}
public void fall() {
y += fallingSpeed;
fallingSpeed = fallingSpeed + gravity;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public boolean isOnGround() {
if (y > 410)
return true;
return false;
}
public void setAttack(boolean attack) {
ableAttack = attack;
}
public void setImageArrLeft() {
move = leftMove.clone();
attack = new ImageIcon("attack.gif");
}
public void setImageArrRight() {
move = movements.clone();
attack = new ImageIcon("attackRight.gif");
}
public void increaseCount() {
count++;
}
public void myDraw(Graphics g) {
if (ableAttack) {
g.drawImage(attack.getImage(), x, y, width + 30, height + 10, null);
}
else {
g.drawImage(move[count % 8].getImage(), x, y, width, height, null);
}
}
}
Then, in my JPanel class, I call the act() method in the Player class under my keyPressed method, checking for VK_ENTER, from the KeyListener interface:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MyPanel extends JPanel implements ActionListener, MouseListener, MouseMotionListener, KeyListener{
int mouseX, mouseY, x = 100, y = 420;
Timer timer;
ImageIcon background = null;
Player p;
public MyPanel() {
timer = new Timer(60, this);
p = new Player((int)(Math.random() * 8), x, y);
background = new ImageIcon("battlefield1.png");
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(background.getImage(), 0, 0, 1375, 750, null);
p.myDraw(g);
repaint();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==timer) {
}
}
public void mouseClicked(MouseEvent me) {}
public void mouseEntered(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mousePressed(MouseEvent me) {}
public void mouseReleased(MouseEvent me) {}
public void mouseDragged(MouseEvent me) {}
public void mouseMoved(MouseEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
p.increaseCount();
if (x < 1300)
x += 10;
p.setX(x);
p.setImageArrRight();
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
p.increaseCount();
if (x > 0)
x -= 5;
p.setX(x);
p.setImageArrLeft();
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
p.setAttack(true);
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
p.act();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
p.setAttack(false);
}
}