0

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

Edward Lim
  • 763
  • 9
  • 31

1 Answers1

1

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.

:)

Community
  • 1
  • 1
Ben
  • 11
  • 2
  • 1
    It's great to refer to another answer that can provide more background, but your answer should be able to stand on its own. To do that, include the relevant bits that would explain how using `&` would solve the problem. – Mogsdad Oct 01 '15 at 02:43
  • I wish i could take a screen shot of what i mean it would be so much simpler. I dont think my question was easy to understand because I think we are kinda off topic here. I do not see how this helps – Edward Lim Oct 01 '15 at 02:50
  • Just replace && for &, because using && you short circuit your logic to only evaluate the first true part. Maybe I should have said that first - sorry. – Ben Oct 01 '15 at 03:04
  • @Mogsdad i fully agree with you but its a classic problem faced by new people here, they dont have permission to comment so to answer the problem like this which need just a comment they have to put this in answer box, but still the answerer explained to its still good – Kumar Saurabh Oct 01 '15 at 06:03
  • I added a screen shot that will hopefully shed some light on the problem im facing, basically when I tap on one half of the screen it is supposed to move the main character upwards by the height of the character however in between these steps something weird happens which is portrayed in my first screenshot – Edward Lim Oct 01 '15 at 19:17