我试图将工具栏按钮固定在屏幕的右上角,但是每当我调整窗口大小时,按钮都将保持在相同的绝对位置(相对于左下角)。
调整大小之前:
调整大小后:
这是生成UI按钮的方法(当前仅在Screen的构造函数中调用):
private void generateUiToolbar(){
// Toolbar Table
Table table = new Table();
table.setFillParent(true);
table.top();
table.right();
// Menu button
table.row();
Texture myTexture = new Texture(Gdx.files.internal("ui/stamp.png"));
TextureRegion myTextureRegion = new TextureRegion(myTexture);
TextureRegionDrawable myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
ImageButton menuButton = new ImageButton(myTexRegionDrawable);
menuButton.addListener( new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
stage.clear();
generateUiMenu();
}
});
table.add(menuButton).size(60,60).padTop(10).padRight(10);
stage.addActor(table);
}
这是resize()方法:
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
stage.getCamera().viewportWidth = width;
stage.getCamera().viewportHeight = height;
stage.getCamera().position.set(stage.getCamera().viewportWidth / 2, stage.getCamera().viewportHeight / 2, 0);
stage.getCamera().update();
}
变量阶段在构造函数中使用以下命令启动 stage = new Stage();
For some reason stage.getViewport().update(width, height, true);
doesn't do anything. When I print stage.getWidth()
after resizing, it will return the same number. What's a good way to update the location of the button or Actors in general? Simply clearing the stage and generating the UI again won't work for me, because sometimes when you resize, there are different actors currently displayed. If I missed any information, please let me know.
EDIT 1: Added table.setFillParent(true);
as mentioned in an answer, but that didn't fix it either.
EDIT 2: After reading the answers and comments, I deleted the stage.getCamera()
manipulation, but I'm still not getting the correct result. This is what happens when I resize with just the line of code stage.getViewport().update(width, height, true);
:
I think you misunderstand the usage of Viewport. In your resize method you do:
stage.getViewport().update(width, height, true);
stage.getCamera().viewportWidth = width;
stage.getCamera().viewportHeight = height;
stage.getCamera().position.set(stage.getCamera().viewportWidth / 2, stage.getCamera().viewportHeight / 2, 0);
stage.getCamera().update();
但是stage.getViewport().update(width, height, true);
已经做好了接下来四行的工作。
viewport.update(width, height)
为相机设置新的宽度和高度并更新相机。所以:
viewport.update(width, height)
is equal to
camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
唯一的区别是视口将根据您使用的视口来查看高宽比。
第三个参数:stage.getViewport().update(width, height, true);
是一个布尔值,表示相机应该居中。因此,如果第三个参数为true,则视口将执行以下操作:
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
在您的调整大小方法中,您将覆盖视口的工作,因为viewport.update(width, height, true)
您已经完成了所有调整大小,并且不需要其他四行。
在resize方法中,这就足够了:
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
您可以阅读此书,以更好地了解视口:相机在Libgdx中的工作方式以及与视口一起使用的方式
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句