2

The text button draws normally (no errors or warnings) but it doesn't respond to mouse clicks. The situation is the same on Desktop and Android build. I've read and followed every topic about it with no results. What am I doing wrong?

Maybe I need to set something in stage object?

Here's my MenuScreen class code:

public class MenuScreen implements Screen {
private OrthographicCamera camera;  
private BitmapFont font;
private TextureAtlas menuGraphics;

Stage stage;
TextButton button;
TextButtonStyle buttonStyle;
Skin skin;

public MenuScreen(Game game)
{
    //create satage object
    stage = new Stage();

    //create font object
    font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"),false);
    font.setColor(Color.RED);

    //load buttons texture atlas and create Skin object
    menuGraphics = new TextureAtlas( Gdx.files.internal( "buttons/buttons.pack" ) );
    skin = new Skin();
    skin.addRegions(menuGraphics);

    // Store the default libgdx font under the name "defaultFont".
    skin.add("defaultFont",font);

    //Set button style
    buttonStyle = new TextButtonStyle();
    buttonStyle.up = skin.newDrawable("yellow");
    buttonStyle.down = skin.newDrawable("orange");
    buttonStyle.checked = skin.newDrawable("orange");
    buttonStyle.over = skin.newDrawable("orange");
    buttonStyle.font = skin.getFont("defaultFont");

    skin.add("default", buttonStyle);

    //assign button style
    button=new TextButton("PLAY",buttonStyle);
    button.setPosition(200, 200);
    button.setSize(200,200);


    //add button to stage
    stage.addActor(button);

    //add click listener to the button
    button.addListener(new ClickListener() {
        public void clicked(InputEvent event, float x, float y) {
            button.setText("Starting new game");
            Gdx.app.log("MenuScreen", "clicked button");

        }
    });

    Gdx.app.log("MenuScreen", "create");
}

@Override
public void resize(int width, int height )
{
    float aspectRatio = (float) width / (float) height;
    camera = new OrthographicCamera(640, 360);
    camera.translate(320,180);
    camera.update();

    stage.setViewport(new FillViewport(640, 360, camera));
    stage.getViewport().setCamera(camera);

    Gdx.app.log( "MenuScreen", "Resizing screen to: " + width + " x " + height );
}

@Override
public void show() {       
    Gdx.input.setInputProcessor(stage);

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    resize((int)w,(int)h);

    Gdx.app.log( "MenuScreen", "Show screen code" );
}

@Override
public void render(float delta)
{
    Gdx.gl.glClearColor( 0f, 1f, 0f, 1f );
    Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );

    stage.act( delta );

    // draw the actors
    stage.draw();       
}

@Override
public void dispose()
{

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

}

fejese
  • 4,601
  • 4
  • 29
  • 36

2 Answers2

4

I Finally solved it!

Additional line in resize function did the trick:

stage.getViewport().update(width, height); 

I realized that the problem was caused by setting the Camera and Viewport in the resize function. When I removed that code (an adjusted the button position to be visible with standard stage viewport) the button started working.

But I wanted to use camera and viewport. Reading about stage resizing in LibGDX documantation (https://github.com/libgdx/libgdx/wiki/Scene2d) I found out that after setting camera, the viewport needs to be updated, otherwise the actor boundries are not recalculated. So I added the viewport update line in resize function and it started working!

The new resize function looks like that:

@Override
public void resize(int width, int height )
{
float aspectRatio = (float) width / (float) height;
camera = new OrthographicCamera(640, 360);
camera.translate(320,180);
camera.update();

stage.setViewport(new FillViewport(640, 360, camera));
stage.getViewport().setCamera(camera);

stage.getViewport().update(width, height);

Gdx.app.log( "MenuScreen", "Resizing screen to: " + width + " x " + height );
}
0

add implements ApplicationListener to your class

public MenuScreen implements ApplicationListener

hope this helps.

Droid
  • 569
  • 4
  • 12
  • I Implemented ApplicationListener in MenuScreen as you adviced but unfortunately it didn't help. Additionally my Game class (that runs this screen) implements ApplicationListener – Christopher Dec 30 '14 at 09:11