文字按钮绘制正常(没有错误或警告),但对鼠标点击没有反应。这种情况在桌面版和安卓版上都是一样的。我已经阅读并关注了所有关于它的主题,但没有结果。我到底做错了什么?
也许我需要在舞台对象中设置一些东西?
下面是我的 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() {
}
}
我终于解决了这个问题
我终于解决了这个问题!在调整大小功能中增加了一行,这就解决了问题。
stage.getViewport().update(width, height);
我意识到这个问题是由在调整大小函数中设置相机和视口引起的。当我删除了这段代码(调整按钮位置,使其在标准舞台视口下可见),按钮开始工作。
但我想使用摄像头和视口。在LibGDX文档中阅读了关于舞台大小调整的内容(https:/github.comlibgdxlibgdxwikiScene2d。)我发现在设置摄像机后,需要更新视口,否则演员的边界不会被重新计算。所以我在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 );
}
在你的类中添加实现ApplicationListener
public MenuScreen implements ApplicationListener
希望能帮到你。