rayan
rayan

Reputation: 49

Move while jumping a square character

I need to be able to move my character while he's jumping.The thing is that I don't want the character to move in a traditional way. He is square and he should move like this when he's on the ground :

enter image description here

I don't want him to stop moving before the animation is finished. But when he jumps and moves at the same time, there's no animation like that. The move becomes classic when he's in the air.

I made a little test code and I managed to get what I want for the character's move on the ground but not for the normal lateral movement in the air.

I should first show you my model (Booby is the character's name):

public class Booby {

    int posX;
    int posY;
    boolean movingRight;
    boolean movingLeft;

    Booby() {
        posX = 0;
        posY = 500;
    }

    int getPosX() {
        return posX;
    }

    int getPosY() {
        return posY;
    }

    void move(int x, int y) {
        posX += x;
        posY += y;
    }
}

Here is my controller :

public class Controller extends KeyAdapter implements ActionListener {
    Timer loop;

    Booby booby;

    boolean right;
    boolean left;
    boolean up;

    int countUp = 0;
    int jump = 0;
    int countLeft = 0;
    int countRight = 0;


    Controller(Booby b, View v) {
        booby = b;
        loop = new Timer(0, this);
    }

    // Key events
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        switch (code) {
        case KeyEvent.VK_UP:
            up = true;
            right = false;
            left = false;
            loop.start();
            break;

        case KeyEvent.VK_RIGHT:
            right = true;
            left = false;
            up = false;
            loop.start();
            break;

        case KeyEvent.VK_LEFT:
            left = true;
            up = false;
            right = false;
            loop.start();
            break;

        }
    }

    public void actionPerformed(ActionEvent evt) {
        if (up) {
            countUp++;
            jump++;
                    // When UP is pressed, it moves up a bit 10 times...
            if (countUp <= 100 && countUp > 0) {
                booby.move(0, -1);
            }
                    // ...Then it moves down a bit 10 times
            else if (countUp > 100) {
                if (jump <= 200) {
                    booby.move(0, 1);
                } else if (jump > 200) {
                    loop.stop();
                    jump = 0;
                    countUp = 0;
                }
            }
        }

            // When Right is pressed, it moves a bit 10 times to the right
        else if (right) {
            booby.movingRight = true;
            countRight++;
            if (countRight <= 315) {
                booby.move(1, 0);
            } else {
                countRight = 0;
                loop.stop();
                booby.movingRight = false;
            }
        }
            // When Leftis pressed, it moves a bit 10 times to the left
        else if (left) {
            booby.movingLeft = true;
            countLeft++;
            if (countLeft <= 315) {
                booby.move(-1, 0);
            } else {
                countLeft = 0;
                loop.stop();
                booby.movingLeft = false;
            }
        }
    }
}

I also have a JPanel which holds the animation :

if (booby.movingRight) {
    imgCharacter = new ImageIcon("images/booby_move_right.gif");
} else if (booby.movingLeft) {
    imgCharacter = new ImageIcon("images/booby_move_left.gif");
} else {
    imgCharacter = new ImageIcon("images/booby.png");
}
Image personnage = imgCharacter.getImage();

g.drawImage(personnage, booby.getPosX() * 1, booby.getPosY() * 1, null);

repaint();

Now, he can move right, left or even jump. But if you press right while he's jumping, it stops the jump and moves to the right.

What I want is that when he's jumping and you press right for example, it move to the right only once. Thereby, if you keep pressing right and you're jumping, it moves slowly to the right.

Upvotes: 3

Views: 984

Answers (2)

Federico Nafria
Federico Nafria

Reputation: 1600

I would take this approach: The right and left key should have no influence in the vertical movement of the character while the jump key should have no influence in the horizontal movement. I think you can change this behaviour by changing how you've "mapped" your keys to the movements. Something like this:

switch (code) {
    case KeyEvent.VK_UP:
        up = true;
        //right = false;
        //left = false;
        loop.start();
        break;

    case KeyEvent.VK_RIGHT:
        right = true;
        left = false;
        //up = false;
        loop.start();
        break;

    case KeyEvent.VK_LEFT:
        left = true;
        //up = false;
        right = false;
        loop.start();
        break;

}

This way, when you press the right/left button it will go right/left but won't stop going up!

Upvotes: 2

kritzikratzi
kritzikratzi

Reputation: 20251

You should be able to fix this by discarding all events until the animation is done, ie. change your keyPressed method to:

public void keyPressed( KeyEvent e ){
    if( loop.isRunning() ){
        // sry, not now :( 
        return; 
    }

    int code = e....

Upvotes: 0

Related Questions