Search code examples
javaandroidgame-enginegame-physics

How To make character legs move in Java


Right now i have MainStarting class where the game loop and paint happens every 60 FPS.

I have 3 states for the character 1- ducking , 2- Jumping and 3-Walking , and every state has it's own picture and i call this picture and paint at a keystroke.

Right now i want his leg to move not just hovering and i dont understand the logic to do so. How many images i need? 60 image to cover the whole 1 cycle of movement [starting from left leg then right leg then stand] cuz of 60 FPS?

Thats the method where i call the pictures

public void init() {

    setSize(800, 480);
    setBackground(Color.BLACK);
    setFocusable(true);
    addKeyListener(this);
    Frame frame = (Frame) this.getParent().getParent();
    frame.setTitle("Q-Bot Alpha");
    try {
        base = getDocumentBase();
    } catch (Exception e) {
        // TODO: handle exception
    }

    // Here are the images , Only 3 images standing and jumping and ducking
    character = getImage(base, "data/character.png");
    characterDown = getImage(base, "data/down.png");
    characterJumped = getImage(base, "data/jumped.png");
    currentSprite = character;
    background = getImage(base, "data/background.png");
    heliboy = getImage(base, "data/heliboy.png");
}

And Here are the game loop

    public void run() {
    while (true) {
        robot.update();
        if (robot.isJumped()) {
            currentSprite = characterJumped;
        } else if (robot.isJumped() == false && robot.isDucked() == false) {
            currentSprite = character; // use switch case in case the character is moving so u can use 10 pictures of the Stick man animation with switch case 
        }

        ArrayList projectiles = robot.getProjectiles();
        for (int i = 0; i < projectiles.size(); i++) {
            Projectile p = (Projectile) projectiles.get(i);
            if (p.isVisible() == true) {
                p.update();
            } else {
                projectiles.remove(i);
            }
        }

        bg1.update();
        bg2.update();
        hb.update();
        hb2.update();
        repaint();
        try {
            Thread.sleep(17);  // To get 60 FPS per second
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

And then i use paint method to just paint. So any help on how could i solve this issue?


Solution

  • If you could clarify your code a bit more that would be great but right now, based on this snipppet,

    character = getImage(base, "data/character.png");
    characterDown = getImage(base, "data/down.png");
    characterJumped = getImage(base, "data/jumped.png");
    currentSprite = character;
    background = getImage(base, "data/background.png");
    heliboy = getImage(base, "data/heliboy.png");
    

    it seems to me that you have a separate sprite for each possible state of your character. What you need are "in-between" sprites/frames to give your character "motion" (right/left leg partially up, right/left leg extended, right/left leg going down).

    I haven't done much game programming in Java, much less for Android, but I think you can lift concepts from Pyganim and start off from there. (Pyganim is in Python, using PyGame.)