0
\$\begingroup\$

I'm making a side-scroller game using LibGDX, where player can move only on X axis. The problem is that the player can only move on positive X value, so he can't go to negative X. This is not that big of a problem, but the main problem is that where-ever I choose his initial position, he will never move right, so he basically can't extend his initial X value. I'm making movement with Gdx.graphics.getDeltaTime(), but when I don't use it, strangely everything works right. Other things work alright, the animation change works, test outputs worked, the problem always occurs only in the part of adding delta time to the x axis.

Player's initial position:

private Actor hiro;
hiro = new Hiro("Hiro", 106, 4, 32, 32);

(I'm not using LibGDX's Actor class, I made my own which uses animations)

Player's constructor:

public Hiro(String name, int x, int y, int width, int height) {
    super(name, x, y, width, height);
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    standingRightTexture = new Texture("hiro_standing_right.png");
    standingLeftTexture = new Texture("hiro_standing_left.png");
    walkingRightTexture = new Texture("hiro_walking_right.png");
    walkingLeftTexture = new Texture("hiro_walking_left.png");
    standingRight = new Animation(standingRightTexture, 1, 10f, true);
    standingLeft = new Animation(standingLeftTexture, 1, 10f, true);
    walkingRight = new Animation(walkingRightTexture, 8, 0.6f, true);
    walkingLeft = new Animation(walkingLeftTexture, 8, 0.6f, true);
    setAnimation(standingRight);
    state = State.STANDING_RIGHT;
    facingRight = true;
}

Player's movement:

if(Gdx.input.isKeyPressed(Input.Keys.D)){
        x += Gdx.graphics.getDeltaTime() * 6f;
        facingRight = true;
        state = State.WALKING_RIGHT;
        System.out.println(x);
    }
    if(Gdx.input.isKeyPressed(Input.Keys.A)){
        x -= Gdx.graphics.getDeltaTime() * 6f;
        facingRight = false;
        state = State.WALKING_LEFT;
        System.out.println(x);
    }

Playing Screen's render method:

@Override
public void render(SpriteBatch spriteBatch) {
    cam.update();
    spriteBatch.setProjectionMatrix(cam.combined);
    spriteBatch.begin();
    hiro.getAnimation().update(Gdx.graphics.getDeltaTime());
    spriteBatch.draw(hiro.getAnimation().getFrame(), hiro.getX(), hiro.getY());
    moveCamera();
    spriteBatch.end();
}

If you want other segments of code, I'll provide it.

I greatly thank everyone whiling to help.

Edit: SOLVED! All I had to do was to use float axis instead of integer. Silly me!

\$\endgroup\$
2
  • \$\begingroup\$ Welcome to GDSE! Here, when we actual find an answer to a question, we actually write an answer and mark it as accepted instead of adding it to the post, and renaming the title to 'solved'. You can write an answer to your own question, and mark is as accepted, it's a perfectly acceptable behaviour here :) And in fact, it is what's expected! \$\endgroup\$ – Vaillancourt Feb 17 '17 at 18:25
  • \$\begingroup\$ Also, self-answering with a valid answer can make your post more interesting to read. :D This will prevent confusion from GDSE users whose looking for unanswered question as well. :) \$\endgroup\$ – Rama Alifiandy Feb 17 '17 at 19:16
0
\$\begingroup\$

If you encounter a similar problem, be sure to always use float axis, not integer ones. When using integer, the game probably had problem with grounding the change of axis, since the Delta Time itself is float. Changing axis to float solved the problem perfectly.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for writing an answer :) Don't forget to come back in a couple of days to mark it as accepted :) \$\endgroup\$ – Vaillancourt Feb 18 '17 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.