Java LibGDX如何在调整窗口大小后更新角色位置?

Java LibGDX如何在调整窗口大小后更新角色位置?,java,user-interface,libgdx,position,Java,User Interface,Libgdx,Position,我试图将工具栏按钮粘贴到屏幕的右上角,但每当我调整窗口大小时,按钮都会保持在相同的绝对位置(相对于左下角) 调整大小之前: 调整大小后: 这是生成UI按钮的方法(当前仅在屏幕的构造函数中调用): private void generateUiToolbar(){ //工具栏表 Table Table=新表(); 表.setFillParent(true); table.top(); table.right(); //菜单按钮 table.row(); 纹理myTexture=新纹理(Gdx.

我试图将工具栏按钮粘贴到屏幕的右上角,但每当我调整窗口大小时,按钮都会保持在相同的绝对位置(相对于左下角)

调整大小之前:

调整大小后:

这是生成UI按钮的方法(当前仅在屏幕的构造函数中调用):

private void generateUiToolbar(){
//工具栏表
Table Table=新表();
表.setFillParent(true);
table.top();
table.right();
//菜单按钮
table.row();
纹理myTexture=新纹理(Gdx.files.internal(“ui/stamp.png”);
纹理区域myTextureRegion=新纹理区域(myTexture);
TextureRegionDrawable myTexRegionDrawable=新的TextureRegionDrawable(myTextureRegion);
ImageButton菜单按钮=新建ImageButton(myTexRegionDrawable);
menuButton.addListener(新建ClickListener()){
@凌驾
已单击公共无效(InputEvent事件、浮点x、浮点y){
stage.clear();
generateUiMenu();
}
});
表.添加(菜单按钮).尺寸(60,60).焊盘(10).焊盘右(10);
阶段。加法器(表);
}
这是resize()方法:

public void resize(int-width,int-height){
stage.getViewport().update(宽度、高度、真值);
stage.getCamera().viewportWidth=宽度;
stage.getCamera().viewportHeight=高度;
stage.getCamera().position.set(stage.getCamera().viewportWidth/2,stage.getCamera().viewportHeight/2,0);
stage.getCamera().update();
}
变量阶段在构造函数中使用
stage=new stage()启动

出于某种原因,
stage.getViewport().update(宽度、高度、真值)什么都不做。调整大小后打印
stage.getWidth()
时,它将返回相同的数字。一般来说,更新按钮或角色位置的好方法是什么?简单地清除舞台并再次生成UI对我来说是行不通的,因为有时当你调整大小时,会显示不同的角色。如果我遗漏了任何信息,请告诉我

编辑1: 添加了
table.setFillParent(true)如回答中所述,但这也无法解决问题

编辑2: 在阅读了答案和评论之后,我删除了
stage.getCamera()
操作,但仍然没有得到正确的结果。这就是当我只使用代码行
stage.getViewport().update(width,height,true)调整大小时发生的情况

视口对象是摄影机平截头体管理器。当您开始修改相机的参数(而不是位置)时,您正在通过调用
viewport.update()
撤消所做的操作


如果您希望表格自动填充屏幕,以便在调整大小后右上角的内容保持不变,请不要在表格上设置宽度和高度。相反,请调用
table.setFillParent(true)
,这样它将自动更新其大小以匹配视口大小。

视口对象是摄影机平截头体管理器。当您开始修改相机的参数(而不是位置)时,您正在通过调用
viewport.update()
撤消所做的操作


如果您希望表格自动填充屏幕,以便在调整大小后右上角的内容保持不变,请不要在表格上设置宽度和高度。相反,请调用
table.setFillParent(true)
,这样它将自动更新其大小以匹配视口大小。

我认为您误解了视口的用法。 在调整大小方法中,您可以执行以下操作:

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(宽度、高度、真值)已经完成了接下来四行中的操作

viewport.update(宽度、高度)
为相机设置新的宽度和高度并更新相机。因此:

viewport.update(width, height)

is equal to

camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
唯一的区别是视口的纵横比取决于您使用的视口

第三个参数:
stage.getViewport().update(宽度、高度、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);
}

为了更好地理解Viewport,您可以阅读以下内容:

我认为您误解了Viewport的用法。 在调整大小方法中,您可以执行以下操作:

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(宽度、高度、真值)已经完成了接下来四行中的操作

viewport.update(宽度、高度)
为相机设置新的宽度和高度并更新相机。因此:

viewport.update(width, height)

is equal to

camera.viewportWidth = width;
camera.viewportHeight = height;
camera.update();
唯一的区别是视口的纵横比取决于您使用的视口

第三个参数:
stage.getViewport().update(宽度、高度、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);
}

为了更好地理解Viewport,您可以阅读以下内容:

谢谢您的解释。你是说我应该删除viewport.update()
?或者我应该做些不同的事情。相机的变化就在那里,所以当我开始调整尺寸时,演员不会开始拉伸。当我执行
table.setFillParent(true)
时,它在调整大小时不会自动更新。。。我错过什么了吗?我假设这是因为视口没有更新?通常您会使用与所需行为匹配的视口(IMO应该是这样的)