简体   繁体   中英

LibGdx multiple touch events confusion

So i have 3 rectangles that act like buttons with the contains method .The first two buttons are just left and right to move the character, but the third one is a jump when user touchDown and then throws a ball when touchUp. My problem is that these get mixed up when the buttons are touched at the same time like when I hold down jump button and at the same time hold and release the left/right button it throws the ball.

I know that there is the pointer id thing, but it's confusing as hell to me.

Set a new InputProcessor to Gdx.input.setInputProcessor(), and check if the touch points are contained in your rectangles, something like this should work:

     Gdx.input.setInputProcessor(new InputAdapter() {
        @Override
        public boolean touchDown(int screenX, int screenY, int pointer, int button) {
            if(leftRect.contains(screenX, screenY)){
                //move player left
                return true;
            }

            if(rightRect.contains(screenX, screenY)){
                //move player right
                return true;
            }

            return false;
        }

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            if(jumpRect.contains(screenX, screenY)){
                //make player jump
                return true;
            }
            return false;
        }
    });

I Strongly recommend you to use Scene2D for ui related stuff, it may be a bit harder to start with, but it will make your ui coding easier to implement and mantain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM