次のエラーが表示されます。
Exception in thread "Thread-3" java.lang.IllegalArgumentException: Comparison method violates its general contract!
Java でエンティティ システムに対してこのコンパレータを実行しようとすると、次のようになります。
private Comparator<Entity> spriteSorter = new Comparator<Entity>() {
public int compare(Entity e0, Entity e1) {
if (e1.position.getX() <= e0.position.getX())
return +1;
if (e1.position.getY() >= e0.position.getY())
return -1;
return 0;
}
};
実装は次のとおりです。
private void sortAndRender(Bitmap b, Vec2 offset, ArrayList<Entity> l) {
Collections.sort(l, spriteSorter);
for (int i = 0; i < l.size(); i++) {
l.get(i).render(b, offset);
}
}
この問題が実際に発生し始めたのは、画面に大量のエンティティを表示していたときだけでした。ここで何が起こっているのですか?