A
A
Arti-Jack2016-12-14 14:02:42
Android
Arti-Jack, 2016-12-14 14:02:42

Why does it not work correctly to draw sprites at the right coordinates?

Good day.
I can't get the sprites ( Texture ) to draw correctly right above the player model (i.e. above the body ).
That is, I need to get something like this:
335106066c7142ae9e87ee01cb5c9fb1.png
But at the same time, I get something completely different:
d90fc0f5584d4d50aa5c3d74ba2dc4ec.png
For some reason it seems to me that this is a matter of incorrect handlers.
That is, I have a PlayScreen class , where I check the user's input and, in accordance with this, load different sprites:

@Override
    public void handleInput(float dt) {

        // do nothing
        player.setStay(true);

        // jumping
        if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) {
            player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
            player.setJump(true);
            player.setStay(false);
            light.setPosition(player.b2body.getPosition().x, player.b2body.getPosition().y);
        }
        // turn right
        if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) &&
                player.b2body.getLinearVelocity().x <= 2) {
            player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true);
           // player.getVelocity().x += Player.SPEED;
            player.setMoveRight(true);
            player.setMoveleft(false);
            player.setStay(false);
        }
        // turn left
        if (Gdx.input.isKeyPressed(Input.Keys.LEFT) &&
                player.b2body.getLinearVelocity().x >= -2) {
            player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true);
            //player.getVelocity().x =- Player.SPEED;
            player.setMoveRight(false);
            player.setMoveleft(true);
            player.setStay(false);
        }

        // escape button - game on pause
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            GameLoader.currentIndex = GameLoader.MENU_PAUSE_STATE;
            GameLoader.gameLoader.addState(new MenuPauseScene());
            GameLoader.gameLoader.setNewState();
        }


    }

And when I jump, the texture also seems to change its y a little , but very slightly.
Perhaps it's the rendering of the texture itself:
// update() вызывается каждый раз при отрисовке
  public void update(float dt) {
        setPosition((b2body.getPosition().x - getWidth() / 2, (b2body.getPosition().y - getHeight() / 2 ));
    }

public void render(SpriteBatch batch, float dt) {
        stateTime += Gdx.graphics.getDeltaTime();

        if (stay) {
            currentFrame = stayAnimation.getKeyFrame(stateTime, true);
            batch.draw(currentFrame, getX() , getY() );
        }

        if (moveRight) {
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);
            batch.draw(currentFrame, getX() , getY() );
        }

        if (moveleft) {
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);
            batch.draw(currentFrame, getX() , getY() );
        }

        if (isJump) {
            currentFrame = walkAnimation.getKeyFrame(stateTime, true);
            batch.draw(currentFrame, getX() , getY() );
        }
}

I just can't figure out why the texture is so "late" behind the CircleShape .

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question