Ok so this is the first time I am making a java game so I am fairly new. The game I am working on now is an overhead game. Anyways I have a visual issues with the sprites where they overlap. I mean its normal to have sprites overlap but I do not want one to appear like it is walking on top of the other rather than the ground. The below is an image illustration the issue I want to address. (I am really good at drawing)
On the right you see what I want my sprites to do, but on left you see overlap in the way I am trying to avoid.
Now my approach to this problem is taking my array of monsters (a class that extends JLabels) and reordering them based on which monster is higher I make this occur at every run of my thread. It seems to me that if a monster is higher it should be added to the screen before the one that is lower. So I try to keep resorting the array of monsters at every iteration of my main game loop based on who is higher to account for the overlap
if (e.getSource() == follower)
{
c =0;
for (monster m: monsters)
{
//m.isTouching(monsters);
m.movingToPlayer(joe);//1
Arrays.sort(monsters, new whoHigherComparator());//2
layeredScene1.add(m, new Integer(3));//3
monsterBumpers[c].attachTo(m, "monster");//4
monsterBumpers[c].handleBumps('m',m,monsterBumpers, monsters);//5
if (m.left == true) m.setIcon(m.animLArr[monAnimCount]);//6
else m.setIcon(m.animRArr[monAnimCount]);//7
//m.movingToPlayer(joe);
c++;//8
}
monAnimCount++;
if (monAnimCount == 20) monAnimCount = 0;
}
The above is code that shows the majority of my games flow I will describe my flow with references to the commented lines above with [num] for better understanding of where I am coming from. Basically, there is a set number of monsters, the monster all locate and move to the player [1], then I attempt to sort the monster array based on height [2], then add my re-sorted monsters back onto the pane in order [3], then bumpers (a type that help me handle collision and make sure monsters don’t overlap too much) attach the current monster [4], if bumpers are touching monsters arae moved away from each other until their bumpers are no longer touching [5], if the monster is moving leftwards set the animation to a leftwards walk [6], and likewise for rightwards walking [7]
By the way this is my comparator
public class whoHigherComparator implements Comparator<monster>
{
@Override
public int compare(monster m1, monster m2) {
System.out.println("comping?");
int h1 = m1.getY();
int h2 = m2.getY();
if (h1 < h2)
{
System.out.println("sss? " + h1 + " vs " +h2 );
return 1;
}
else if (h2 > h1)
{
System.out.println("sss$");
return -1;
}
return 0;
}
}
At this point everything in my game flow works just fine, its just the annoying “walking on top of each other” effect that I am having trouble handling. And my attempt at this is line [2] & [3] from my game flow which seems like it would be an effective way to handle the “walking on top of another” effect but its surely not working. I was wonder if you guys have any idea what I can do to tweak my code to effectively handle this situation. Thank you for reading through, and any help is greatly appreciated. Cheers