Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Libgdx – Jumping, What am i doing wrong?

$
0
0

I really can’t work out whats wrong with my code. I have an input listener for gestures, when I Swipe the position goes up on the y axis fine but he doesn’t come back down, if with Gravity added. I’ll show you any code relevant to the Character.

World.java : updateRuben

private void updateRuben (float deltaTime, float accelX) {


        if (Ruben.getState() != Ruben.RUBEN_STATE_IDLE && ruben.position.y <= 0.5f) ruben.hitPlatform();//
        if (Ruben.getState() != Ruben.RUBEN_STATE_IDLE) ruben.velocity.x = -accelX / 10 * Ruben.RUBEN_MOVE_VELOCITY;

                // clamp the velocity to 0 if it's < 1, and set the state to standing
                if (Math.abs(ruben.velocity.y) < 1) {
                    ruben.velocity.y = 0;
                    Ruben.setState(Ruben.RUBEN_STATE_IDLE);
                }



        ruben.update(deltaTime);
        heightSoFar = Math.max(ruben.position.y, heightSoFar);
    }

Ruben.java

package com.wilson.game.Game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.input.GestureDetector;
import com.wilson.game.System.DynamicGameObject;
import com.wilson.game.System.World;
import com.wilson.game.System.inputController;

public class Ruben extends DynamicGameObject {
    public static final int RUBEN_STATE_IDLE = 0;
    public static final int RUBEN_STATE_JUMP = 1;
    public static final int RUBEN_STATE_FALL = 2;
    public static final int RUBEN_STATE_HIT = 3;
    public static final int RUBEN_STATE_WALKING = 4;
    public static final int RUBEN_STATE_SPAWN = 5;

    public static final float RUBEN_MAX_VELOCITY = 11;
    public static final float RUBEN_JUMP_VELOCITY = 11;
    public static final float RUBEN_MOVE_VELOCITY = 20;
    public static final float RUBEN_WIDTH = 2f;
    public static final float RUBEN_HEIGHT = 2.5f;
    public static final float RUBEN_SPAWN_TIME = 0.4f;

    public static int state;
    static float stateTime;
    private static boolean grounded;
    public boolean facingRight;

    public Ruben (float x, float y) {
        super(x, y, RUBEN_WIDTH, RUBEN_HEIGHT);
        state = RUBEN_STATE_SPAWN;
        stateTime = 0;
        grounded = true;
        facingRight = true;

        Gdx.input.setInputProcessor(new GestureDetector(new inputController()));
        }

    public void update (float deltaTime) {
        processInput(); 
        position.add(velocity.x * deltaTime, velocity.y * deltaTime);
        velocity.add(World.gravity.x * deltaTime, World.gravity.y * deltaTime);

        bounds.x = RUBEN_WIDTH;
        bounds.y = RUBEN_HEIGHT;

        if (state == RUBEN_STATE_SPAWN) {
            if (stateTime > RUBEN_SPAWN_TIME) {
                state = RUBEN_STATE_IDLE;
                grounded = true;
            }
        }

        if (position.x * deltaTime < 0) position.x = World.WORLD_WIDTH;
        if (position.x * deltaTime > World.WORLD_WIDTH) position.x = 0;

        stateTime += deltaTime;
}

    private void processInput() {
        if (inputController.fling == true) {
            jump();
            }
        }

    public void hitSquirrel () {
        velocity.set(0, 0);
        state = RUBEN_STATE_HIT;
        stateTime = 0;
    }

    public void jump() 
    {
        if (state != RUBEN_STATE_JUMP)
        state = RUBEN_STATE_JUMP;
        velocity.y = velocity.y * RUBEN_JUMP_VELOCITY;
        grounded = false;
        stateTime = 0;
    }

    public void hitPlatform () {
        state = RUBEN_STATE_IDLE;
        stateTime = 0;
        grounded = true;
    }

    public void hitSpring () {
        velocity.y = RUBEN_JUMP_VELOCITY * 1.5f;
        state = RUBEN_STATE_JUMP;
        stateTime = 0;
        grounded = false;
    }

    public static int getState() {
        return state;
    }

    public static float getStateTime() {
        return stateTime;
    }

    public static void setState(int currentstate) {
        currentstate = state;
    }

    public static boolean isGrounded() {
        return grounded;
    }

    public static void setStateTime(float i) {

        i = stateTime;

    }
}

How do you apply physics to the actor in order for him to come back down? what am i missing?


Viewing all articles
Browse latest Browse all 434

Trending Articles