本文介绍了如何在 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 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 18:07