How do you sort Actors in a libgdx Stage?(如何在 libgdx Stage 中对 Actor 进行排序?)
问题描述
我在对 LibGdx Stage 对象中的 Actor 进行排序时遇到问题.当舞台被渲染时,图像会按照它们添加的顺序被渲染.Stage 使用一个数组来保存 Actor.我已经尝试设置每个 Actor 的 ZIndex,但它仍然没有排序.然后我尝试像这样创建一个比较器对象:
I'm having trouble sorting Actors in a LibGdx Stage object. When the Stage gets rendered the images are rendered in the order they are added. Stage uses an Array to hold the Actors. I've tried setting the ZIndex of each Actor, but it still didn't sort. Then I tried creating a comparator object like this:
public class ActorComparator implements Comparator < Actor > {
@Override
public int compare(Actor arg0, Actor arg1) {
if (arg0.getZIndex() < arg1.getZIndex()) {
return -1;
} else if (arg0.getZIndex() == arg1.getZIndex()) {
return 0;
} else {
return 1;
}
}
}
然后当我想进行实际比较时:
and then when I want to do the actual comparison I did:
Collections.sort(Stage.getActors(), new ActorComparator());
它给了我以下错误并且不会编译:
It gives me the following error and won't compile:
The method sort(List<T>, Comparator<? super T>) in the type Collections
is not applicable for the arguments (Array<Actor>, ActorComparator)
我不知道我做错了什么.谁能给我解释一下?
I have no clue what I'm doing wrong. Can someone explain this to me?
推荐答案
看起来你的代码 Stage.getActors()
返回一个 Array
of Actors
而不是 List
.Collections.sort()
方法只接受列表.试试:
Looks like your code Stage.getActors()
returns an Array
of Actors
instead of a List
.
Collections.sort()
method accepts only Lists.
Try:
Collections.sort(Arrays.asList(Stage.getActors().toArray()), new ActorComparator());
@James Holloway 的排序更新(按问题中的 z-index):z-index 被阶段覆盖为数组的内部顺序.因此,设置 Z-Index 没有任何效果,除非您将其设置为高于列表的长度,然后它只是将图像放在列表的顶部(内部 Stage 会这样做).这可以通过按名称或 ID 排序来解决.
Update on sorting (by z-index in the question) from @James Holloway: z-index is overridden by the stage to be whatever the internal order of the Array is. So setting the Z-Index has no effect, except in the case that you set it as higher than the length of the list and then it just puts the image on top of the list (internal Stage does this). This is solved by sorting by name or ID.
这篇关于如何在 libgdx Stage 中对 Actor 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 libgdx Stage 中对 Actor 进行排序?


- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01