Search code examples
javalibgdx

How do i link a Texture image button to another screen?


1) How do I set the "playbuttonactive" to go to the gamescreen upon clicking on the texture and as for "exitbtn" texture would be exiting the game?

enter image description here


Solution

  • I'll assume that playbuttonActive and exitbutton are textures that are drawn on the screen. If that's the case you just need to check where the screen was touched:

    if (Gdx.input.isTouched()) {
      Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); // get the position of the input event
      camera.unproject(touchPos); // converts screen coordinates to world coordinates
      
      if (isWithinPlayButtonActiveCoordinates(touchPos)) {
        // change to the game screen
        game.setScreen(new GameScreen(game));
        dispose();
      }
      else if (isWithinExitButtonCoordinates(touchPos)) {
        // close the application
        Gdx.app.exit();
      }
    }