你如何在libgdx阶段对Actors进行排序? [英] How do you sort Actors in a libgdx Stage?

查看:147
本文介绍了你如何在libgdx阶段对Actors进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LibGdx Stage对象中排序Actors时遇到问题。渲染舞台时,图像将按照添加顺序进行渲染。 Stage使用Array来保存Actors。我已经尝试设置每个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()返回数组 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阶段对Actors进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆