在 libgdx
中为您提供了一个复杂的 Scene2D
图,其中包含多个 Group's
和 Actor's
.您希望用户选择一些 Actors
并在最后绘制它们,以便它们看起来集中在任何其他 Actors
之上.
You are given a complex Scene2D
graph in libgdx
with several Group's
and Actor's
. You want the user to select some Actors
and draw those at the end so they appear focussed on top of any other Actors
.
我想迭代 Stage
两次.第一次绘制未选中的Actors
,第二次绘制选中的actors
.但是,我没有看到任何好的"方法来强制执行此行为.
I would like to iterate over the Stage
twice. The first time it draws the unselected Actors
, the second time it draws the selected actors
. I don't see any 'good' way to enforce this behaviour however.
我更喜欢干净的选项.我不想为了这个小小的添加而复制整个方法实现.
I would prefer options that are clean. I would not like to copy an entire method implementation just for the sake of this small addition.
什么不起作用:
Actor's
toFront
() 方法只适用于它的兄弟姐妹.Actor's
的位置:这会修改 Actors
的转换.Actor's
toFront
() method only works for it's siblings. Actor's
in the Stage: this modifies the transformations the Actors
have.需要考虑的场景:您有一个 Root
,其中包含一个组 gA 和一个组 gB.Group
gA 包含两个图像 iA1 和 iA2.Group
gB 包含一个图像 iB.假设 Group
gA 首先添加到舞台,并且图像 iA1 与 iB 重叠;现在您要选择 iA1 并使其出现在 iB 上.我不想只调用 gA.toFront();这会将整个 Group
gA 放在前面,这意味着 iA2 也放在前面.将 iA2 放在前面有将部分图像隐藏在 Group
gB
Scenario to think about: you have a Root
with a group gA and a group gB. Group
gA contains two images iA1 and iA2. Group
gB contains one image iB. Given that Group
gA is added first to the stage and that image iA1 overlaps with iB; now you want to select iA1 and make it appear over iB. I don't want to only call gA.toFront(); this would put the whole Group
gA to the front, which means also iA2 is put to the front. Putting iA2 in front has the undesired effect of hiding parts of images inside Group
gB
有两种解决方案-
1 - 停止使用 [多个] 组.糟透了,但这可能是更容易的选择.如果您查看渲染/绘图是如何完成的,您将从舞台的根组开始,并获取其子级并渲染它们.对于这些孩子中的每一个,如果它们是一个组,则呈现该组的孩子.ZIndex 只不过是一个组内的孩子的顺序.如果您查看 Actor 的 setZIndex
,您就会明白为什么 toFront
或 setZIndex
只影响兄弟姐妹.
1 - Stop using [multiple] groups. Sucks but this may be the easier option. If you look at how rendering/drawing is done you start at the root Group for a Stage and get its children and render them. For each of those children, if they are a Group then render that Groups children. ZIndex is nothing more than the order of children within a group. If you look at the Actor's setZIndex
you can see why toFront
or setZIndex
only affect siblings.
public void setZIndex (int index) {
if (index < 0)
throw new IllegalArgumentException("ZIndex cannot be < 0.");
Group parent = this.parent;
if (parent == null)
return;
Array<Actor> children = parent.getChildren();
if (children.size == 1)
return;
if (!children.removeValue(this, true))
return;
if (index >= children.size)
children.add(this);
else
children.insert(index, this);
}
2 - 唯一的其他选择是更改所有演员的绘制顺序.您必须扩展 Stage
并根据您选择的不同顺序将 draw
方法替换为 draw
.您可能必须合并 Group.drawChildren
方法中的许多功能.
2 - The only other option would be to change the drawing order of all the actors. You'd have to extend Stage
and replace the draw
method to draw
based on a different order of your choosing. You'd probably have to incorporate a lot of the functionality from the Group.drawChildren
method.
TLDR;LibGDX 中的实现方式 - 组是一个层.如果您不想要图层,请更改组的功能或停止使用组.
TLDR; The way things are implemented in LibGDX - a Group is a layer. If you don't want layers then either change what groups do or stop using groups.
这篇关于覆盖绘图顺序scene2D的舞台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!