Edward Lim
Edward Lim

Reputation: 823

Character not drawing correctly, appears in the wrong spot for a second

So here is my problem, Im not sure whats the deal here but its supposed to be pretty straightforward. I have a texture that i want to draw either on the left half of the screen or the right half of the screen. When the corresponding side of the screen is touched

METHOD THAT CALLS THE METHOD:

public void screenTouched() {

    if (Gdx.input.justTouched()) {
        main_char.updateMainCharacter(Gdx.input.getX(), main_char.getPosition().y);
    }

METHOD THAT MOVES THE TEXTURE:

public void updateMainCharacter(float touch_pos_x, float pos_y) {

        //main_char is already on left side
        if (position.x < ClimberDude.V_WIDTH / 2 && touch_pos_x < ClimberDude.V_WIDTH / 2) {
            position.x = 0;
            position.y = pos_y + main_char.getHeight();
        }
        //main_char is already on right side
        else if (position.x > ClimberDude.V_WIDTH / 2 && touch_pos_x > ClimberDude.V_WIDTH / 2) {
            position.x = ClimberDude.V_WIDTH - main_char.getWidth();
            position.y = pos_y + main_char.getHeight();
        }
        else if (touch_pos_x < ClimberDude.V_WIDTH / 2) {
            position.x = 0;
            position.y = pos_y;
        }
        else if (touch_pos_x > ClimberDude.V_WIDTH / 2) {
            position.x = ClimberDude.V_WIDTH - main_char.getWidth();
            position.y = pos_y;
        }

    }

THIS IS THE PICTURE OF THE PROBLEM enter image description here

enter image description here

Upvotes: 0

Views: 60

Answers (1)

Ben
Ben

Reputation: 11

The problem lies in your choice of operators && as opposed to &.

Ok here it is:

It moves because only one part of his double conditional is being processed per loop. So if he removes the && from each he will quickly see improvement in his logic.

See here: Difference between & and && in Java?

This will explain everything for you.

:)

Upvotes: 1

Related Questions