问题
I need to resort a SnapShotArray so I may render my actors in the correct order. Ordering should be an absolute distance from a center, though I want to hold on to sign for later in the rendering process to determine whether it put to the left or right of center. I determined distance from center for each actor in the first for loop, and tried using setUserObject(). When I later tried to retrieve this in the sort Comparator I was told it was a null value.
SnapshotArray<Actor> children = getChildren();
children.begin();
int n = children.size;
for (int i = getIndexOfFocus() + 1; i < n; i++) {
Actor child = children.get(i);
int offset = i - Math.round(getIndexOfFocus());
child.setUserObject(Integer.valueOf(offset));
}
//Arrays.sort(<SnapShotArray>children, 0, n, new MyElementComparator());
children.sort(new MyElementComparator());
Comparator class:
private static class MyElementComparator implements Comparator<Actor>{
public int compare (Actor a1, Actor a2){
Integer e1 = (Integer) a1.getUserObject();
Integer e2 = (Integer) a2.getUserObject();
System.out.println("1(" + a1 + ", " + e1 + "), 2(" + a2 + ", " + e2 + ")");
int r = 0;
if (Math.abs(e1) < Math.abs(e2)) r = -1;
if (Math.abs(e1) == Math.abs(e2)) r = 0;
if (Math.abs(e1) > Math.abs(e2)) r = 1;
return r;
}
}
results of Sys out above:
1(TextButton: index_1, null), 2(TextButton: index_0, null)
来源:https://stackoverflow.com/questions/62598312/libgdx-setuserobject-not-setting-object