LibGDX TextButton不响应鼠标点击

时间:2014-12-30 01:36:10

标签: button libgdx stage mouseclick-event

文本按钮正常绘制(没有错误或警告),但它不响应鼠标点击。桌面和Android版本的情况相同。我已经阅读并关注了有关它的所有主题,但没有结果。我究竟做错了什么?

也许我需要在舞台对象中设置一些东西?

这是我的MenuScreen班级代码:

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() {

}

}

2 个答案:

答案 0 :(得分:3)

我终于解决了!

调整大小功能中的附加行就是诀窍:

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

我意识到问题是由调整大小功能中的Camera和Viewport引起的。当我删除该代码(调整按钮位置以使用标准舞台视口可见)时,按钮开始工作。

但我想使用相机和视口。在LibGDX文档(https://github.com/libgdx/libgdx/wiki/Scene2d)中阅读关于阶段大小调整的内容我发现在设置相机后,需要更新视口,否则不会重新计算actor边界。所以我在resize函数中添加了视口更新行,它开始工作了!

新的resize功能如下所示:

@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 );
}

答案 1 :(得分:0)

将实现ApplicationListener添加到您的类

public MenuScreen implements ApplicationListener

希望这会有所帮助。