Skip to main content

You have the freedom to create your own button class, attach to it a sprite, make some methods to handle input and youryou're good to go.

    public class SimpleButton {
    
    private Sprite skin; 
    
    public SimpleButton(Texture texture, float x, float y, float width, float height) {
        skin = new Sprite(texture); // your image
        skin.setPosition(x, y);
        skin.setSize(width, height);
    }
    
    public void update (SpriteBatch batch, float input_x, float input_y) {
        checkIfClicked(input_x, input_y);
        skin.draw(batch); // draw the button
    }
    
    private void checkIfClicked (float ix, float iy) {
        if (ix > skin.getX() && ix < skin.getX() + skin.getWidth()) {
            if (iy > skin.getY() && iy < skin.getY() + skin.getHeight()) {
                // the button was clicked, perform an action
                System.out.println("Button clicked !");
            }
        }
    }

}

For the screenscreen size issue you could use percentage of the screen size  :

public float convertToPercents_width (float p) {
    return Gdx.graphics.getWidth()*p/100;
}

public float convertToPercents_height (float p) {
    return Gdx.graphics.getHeight()*p/100;
}
 

 

You have the freedom to create your own button class, attach to it a sprite, make some methods to handle input and your good to go.

    public class SimpleButton {
    
    private Sprite skin; 
    
    public SimpleButton(Texture texture, float x, float y, float width, float height) {
      skin = new Sprite(texture); // your image
      skin.setPosition(x, y);
      skin.setSize(width, height);
    }
    
    public void update (SpriteBatch batch, float input_x, float input_y) {
        checkIfClicked(input_x, input_y);
        skin.draw(batch); // draw the button
    }
    
    private void checkIfClicked (float ix, float iy) {
        if (ix > skin.getX() && ix < skin.getX() + skin.getWidth()) {
            if (iy > skin.getY() && iy < skin.getY() + skin.getHeight()) {
                // the button was clicked, perform an action
                System.out.println("Button clicked !");
            }
        }
    }

}

For the screen size issue you could use percentage of the screen size  :

public float convertToPercents_width (float p) {
    return Gdx.graphics.getWidth()*p/100;
}

public float convertToPercents_height (float p) {
    return Gdx.graphics.getHeight()*p/100;
}

You have the freedom to create your own button class, attach to it a sprite, make some methods to handle input and you're good to go.

public class SimpleButton {
    
    private Sprite skin; 
    
    public SimpleButton(Texture texture, float x, float y, float width, float height) {
        skin = new Sprite(texture); // your image
        skin.setPosition(x, y);
        skin.setSize(width, height);
    }
    
    public void update (SpriteBatch batch, float input_x, float input_y) {
        checkIfClicked(input_x, input_y);
        skin.draw(batch); // draw the button
    }
    
    private void checkIfClicked (float ix, float iy) {
        if (ix > skin.getX() && ix < skin.getX() + skin.getWidth()) {
            if (iy > skin.getY() && iy < skin.getY() + skin.getHeight()) {
                // the button was clicked, perform an action
                System.out.println("Button clicked !");
            }
        }
    }

}

For the screen size issue you could use percentage of the screen size:

public float convertToPercents_width (float p) {
    return Gdx.graphics.getWidth()*p/100;
}

public float convertToPercents_height (float p) {
    return Gdx.graphics.getHeight()*p/100;
}
 

 

Source Link
Ion Farima
  • 716
  • 7
  • 19

You have the freedom to create your own button class, attach to it a sprite, make some methods to handle input and your good to go.

    public class SimpleButton {
    
    private Sprite skin; 
    
    public SimpleButton(Texture texture, float x, float y, float width, float height) {
      skin = new Sprite(texture); // your image
      skin.setPosition(x, y);
      skin.setSize(width, height);
    }
    
    public void update (SpriteBatch batch, float input_x, float input_y) {
        checkIfClicked(input_x, input_y);
        skin.draw(batch); // draw the button
    }
    
    private void checkIfClicked (float ix, float iy) {
        if (ix > skin.getX() && ix < skin.getX() + skin.getWidth()) {
            if (iy > skin.getY() && iy < skin.getY() + skin.getHeight()) {
                // the button was clicked, perform an action
                System.out.println("Button clicked !");
            }
        }
    }

}

For the screen size issue you could use percentage of the screen size :

public float convertToPercents_width (float p) {
    return Gdx.graphics.getWidth()*p/100;
}

public float convertToPercents_height (float p) {
    return Gdx.graphics.getHeight()*p/100;
}