0

I am making a game where I can spawn soldiers with a button click. All of these soldiers will be added in an ArrayList named entity_walking_right_array. I also have another ArrayList for the entities which are walking left.

I have another class where the x, y position, the animation etc. of the soldier is determined or drawn. All soldiers have a random Y value between the screen height and 450 pixels.

When I spawn the soldiers some of them will logically have a higher y value or y position than others but when those are created later, than soldiers with a lower y value, they will be rendered above them. That leads to soldiers being rendered above soldiers when they logically would be behind them. (For example the feets are in the face of the lower y value soldier)

I then render the soldiers like that:

for (entity_walking_right blue : entity_walking_right_array) {
    blue.render(game.batch);
}

I figured out that when I use the y value of those soldiers ( entity_walking_right.getY(); is a float) to sort the ArrayList then they should be rendered in the right order. At least I am hoping that is the right way to do it.

I have already tried using entity_walking_right_array,sort(...), Collections.sort or some of those Lambda functions which are completely new to me. I've also tried using compareTo (I unfortionatly cannot find the question anymore) but it gave me an error which basically said that compareTo just does not exist as a function.
Any code that I've found did not really help me and I am trying to get this problem solved for 2 days now which is the reason why I am writing the question.

Edit:
I have found the question.
Sort ArrayList of custom Objects by property
When I try to use

Collections.sort(entity_walking_right_array,
    (o1, o2) -> o1.getY().compareTo(o2.getY()));

it simply says "Cannot resolve method 'compareTo(float)'.

dStulle
  • 609
  • 5
  • 24
Daniel M
  • 746
  • 5
  • 13
  • Come on over to the LibGDX forum and we can help you out. You're running into issues with Comparable vs. Comparator and Java 7 vs Java 8 (which is only partially supported on Android Marshmallow (6) and below). And LibGDX has its own collections that are better suited to games than Java's built-in collections like ArrayList. – Tenfour04 Jun 17 '19 at 14:33

2 Answers2

0

it simply says "Cannot resolve method 'compareTo(float)'. You need to use method compare instead of compareTo and pass as parameters the two objects you want compare. https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Collections.sort(entity_walking_right_array, (o1, o2) -> compare(o1.getY(), o2.getY()));

lampaDT
  • 13
  • 1
  • search in internet for "Comparator" and "Comparable", they are two different interfaces, you are confusing them. – lampaDT Jun 17 '19 at 11:45
  • Thanks for the help but unfortionately the App crashes and it shows me this Error: FATAL EXCEPTION: GLThread 55410 Process: com.daenni.mygdx.game, PID: 13031 java.lang.BootstrapMethodError: Exception from call site #0 bootstrap method

    I have also noticed that I need a minimum API of 26 (Android 8.0). Is there a better way to do this so i could also include Android 7.0? That would be my goal.
    – Daniel M Jun 17 '19 at 12:51
0

When comparing a primitive type you can't use compareTo as in the linked question so you need to write your own or used a predefined one

entity_walking_right_array.sort(Comparator.comparingDouble(entity_walking_right::getY));

or

entity_walking_right_array.sort(Comparator.comparingDouble(entity_walking_right::getY).reversed());

depending on sort order.

And please use java standard naming, entity_walking_right -> entityWalkingRight

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • Why the down vote? – Joakim Danielson Jun 17 '19 at 12:41
  • @DanielM Any feedback on this? – Joakim Danielson Jun 17 '19 at 14:07
  • Sorry, I am still trying to solve it. I have tried your solution and it is actually the first time that the program compiled since I am trying to solve this problem. Kudos to you. The problem now is that it actually does nothing. I have tried this: `if(entity_walking_right_array.size() >= 2){ message = "SortArray"; entity_walking_right_array.sort(Comparator.comparingDouble(entity_walking_right::getY)); }` But the soldiers are still rendering above each other in the wrong way. I don't see any difference with or without that code. – Daniel M Jun 17 '19 at 14:26
  • @DanielM I don't know what you do with the array list afterwards so I can't really help you but did you try a printing the content of the array after the sort in a simple for loop to check that the list has been sorted? – Joakim Danielson Jun 18 '19 at 11:38
  • I don't really do anything after that. I'll just render them after that: `//Render entityWalkingRight for (entityWalkingRight blue : entityWalkingRightArray) { blue.render(game.batch); }` – Daniel M Jun 18 '19 at 12:23
  • Is it just a typo that the array has different names in your last two comments? – Joakim Danielson Jun 18 '19 at 12:36
  • No, I changed the names today as you recommended me to use java standard naming. – Daniel M Jun 18 '19 at 13:50
  • In the meantime I have printed out the content of the Array. The code that I wrote should show the Index on the left site and the corrosponding value on the right side. I tested it till one entity overlapped another one when he shouldn't and stopped the app. This is the output: https://www.minpic.de/i/7y4e/3h9ds As aspected the code did not change them. – Daniel M Jun 18 '19 at 14:51
  • But with that output you can’t tell the difference between them. Why not print getY and/or something that identifies the objects? – Joakim Danielson Jun 18 '19 at 14:55
  • I actually did not think about that, dumb me. I have now tested it again and got like the "perfect wrong result" The three soldiers are rendered above each other in the wrong order. https://www.minpic.de/i/7y4g/u7mp1 – Daniel M Jun 18 '19 at 15:05
  • And the second part of my answer, did you consider trying that? – Joakim Danielson Jun 18 '19 at 15:12
  • I've made a breakthrough! I have just changed your code from `entityWalkingRightArray.sort(Comparator.comparingDouble(entity_walking_right::getY).reversed());` to `entityWalkingRightArray.sort(Comparator.comparing(entity_walking_right::getY).reversed());` just to see what happens and he starts sorting them! The problem is that he is sorting them in the wrong order. But when I add `...(entity_walking_right::getY).reversed());` then he tells me "Cannot resolve method 'getY'" – Daniel M Jun 18 '19 at 15:15
  • I finally solved it! I just don't use `.reversed()`, I just don't give him a positive Y position. I multiply it by ` * (-1)` and now he sorts it in the right way. Thank you very much for the help! – Daniel M Jun 18 '19 at 15:22