简体   繁体   English

如何使用Scene2d和LibGDX集中一个组中的actor?

[英]How to center actors in a group with Scene2d and LibGDX?

I'm using LibGDX and Scene2d and I want to create a pop-up that looks like the one shown in the image below. 我正在使用LibGDX和Scene2d,我想创建一个看起来像下图所示的弹出窗口。

So far, I created a table and set the background to a semitransparent color. 到目前为止,我创建了一个表格,并将背景设置为半透明颜色。 Then I created an image with a white background. 然后我创建了一个白色背景的图像。 This image expands x, so it is centered. 此图像展开x,因此它居中。 Now I want to have a label centered inside the image. 现在我想在图像中心放置一个标签。 To overlay these two actors, I created a group, but I couldn't find out how to center something in a group. 为了覆盖这两个演员,我创建了一个小组,但是我无法找到如何将小组集中在一个小组中。 How can I do this? 我怎样才能做到这一点?

scene2d的例子

Here is my code: 这是我的代码:

Table table = new Table();
table.top();
table.setFillParent(true);
table.setBackground(getColoredBackground(color));

labelStyle = new Label.LabelStyle(font, fontColor);
label = new Label(message, labelStyle);

Image image = new Image(whiteBackground);
image.setSize(table.getWidth() - padding, label.getHeight() + extraHeight);

Group group = new Group();
group.setSize(image.getWidth(), image.getHeight());

group.addActor(image);
group.addActor(label);
table.add(group).expandX().padTop(padTop);

The group has it own coordinates system. 该小组拥有自己的坐标系。 It means that when you are setting actor position to (0,0) for example and then adding it to group which is actually at (100, 100) position then the object will be at (100, 100) position relative to the stage . 这意味着当您将actor位置设置为(0,0),然后将其添加到实际位于(100,100)位置的组时, 该对象将位于相对于舞台的(100,100)位置

All you need to do is just to set: 您需要做的就是设置:

    label.setPosition( group.getWidth() / 2f - label.getWidth() / 2f, group.getHeight() / 2f - label.getHeight() / 2f );

after setting Group size. 设置大小后。 Remember that setting actor's position we are defining its left bottom corner position and that's why you need to also subtract the half of label width/height . 请记住,设置actor的位置我们正在定义它的左下角位置,这就是为什么你还需要减去标签宽度/高度的一半

Of course you can modify the y position it doesn't have to be centered vertically also 当然,您也可以修改它不必垂直居中的y位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM