0

I am having 3 different screens which contains splash screen, after menu screen and game screen. Splash > Menu > Gamestarts.

How can i add an image button ??

I want to implement 3 buttons inside Menu screen, Not getting any idea where to start.

public class MenuScreen implements Screen {


private Spacegame game;
private SpriteBatch batch;
private Sprite sprite;
private Texture texture;
TextureRegion bg,play,spacegamelogo,button;
OrthographicCamera camera;
Vector3 touchPoint;
private Skin buttonskin;
public MenuScreen(Spacegame game)
{
    touchPoint = new Vector3();
    this.game=game;
    batch=new SpriteBatch();

    bg=AssetLoader.bg;
    spacedebrislogo=AssetLoader.spacedebrislogo;
    button=AssetLoader.button;
}
    @Override
public void show() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
   camera = new OrthographicCamera(1, h / w);

    sprite = new Sprite(bg);
    sprite.flip(false, true);
    sprite.setSize(1.0f,
            1.0f * sprite.getHeight() / sprite.getWidth() );
            sprite.setOrigin(sprite.getWidth() / 2,
            sprite.getHeight() / 2);
            sprite.setPosition(-sprite.getWidth() / 2,
            -sprite.getHeight() / 2);
}
@Override
public void render(float delta) {

    batch.setProjectionMatrix(camera.combined);
    batch.begin();

    sprite.draw(batch);

    batch.draw(spacedebrislogo, 33, 54, 50, 40);
    batch.end();
    if (Gdx.input.isTouched()) {

        game.setScreen(new GameScreen());
        dispose();
    }

}
CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
Biswatma
  • 107
  • 4
  • 17
  • Are you planning to use Scene2D with Scene2D.ui or create your own widgets? – EpicPandaForce Jun 23 '14 at 11:05
  • i already created screen,now backgroundimage is displaying, above that i want to add play button. What is Scene2d? – Biswatma Jun 23 '14 at 11:18
  • Scene2D is LibGDX's scene graph. If you use the 'Scene' provided by LibGDX to display stuff, then you can use the Widgets that work as "Actors" which are managed by the Scene. However, if you don't want to resort to Scene2D, then you need to create your own buttons. I posted my button implementation here: http://stackoverflow.com/questions/24219170/libgdx-custom-click-listener/24219776#24219776 – EpicPandaForce Jun 23 '14 at 11:22

1 Answers1

1

THere are a lot of methods to do it.. I will tell you the way I do. First I create my button image, add it to the assets folder and load the texture region. Now I make a sprite out of it.

Sprite button1=new Sprite(myTextureRegion);

To check if the button is touched I can use the rectangle from the sprite to check if you touched the image. In your touchUp method you will do something like

if(button1.getBoundingRectangle.contains(screenX,screenY))
  // do your thing

To make my game more interesting i like to add some rotation or scaling of my sprite when is clicked, so it looks better, you can play with it, or you can make 2 textures, one for touched down and one for touched up.

Boldijar Paul
  • 5,405
  • 9
  • 46
  • 94
  • Smart. The question is, do you have the text written on the texture itself? – EpicPandaForce Jun 23 '14 at 11:40
  • Yes. Use inkscape, is very easy to use and works very good. I don't recommend using a texture for button and text for separate buttons.. is just too hard to do it and won't look that good..design your own button, there are a lot of tutorials out there :) – Boldijar Paul Jun 23 '14 at 14:43
  • Do you load the textures differently based on the size of the screen? I personally just used an Ortho2D transform, but that has its problems. – EpicPandaForce Jun 23 '14 at 18:39
  • Well my game fixed resolution is 800x480. I provide assets for SD,MD,HD,XHD graphics. I set my sprite size according to the texture size on the MD asset. Actually i only make graphics for XHD, and using my program i scale them down for the rest of the resolutions http://www.badlogicgames.com/forum/viewtopic.php?f=12&t=14989 . Anyways, something like 800x480 fixed resolutions and graphics just for that size will work good on 90% of the phones.. you would need hd for tablets i guess – Boldijar Paul Jun 23 '14 at 19:38