Draw pictures by y positions

Hello. The situation needs help.

I (for the game) need the characters to be drawn gradually (as if the game understands which character is closer to the camera and which is not).
And at the same time, that there were rendering layers.

While there were few objects, I managed the usual double for () in height (from top to bottom I checked each object to see if it matches the desired height, if so, I drew).

for(int i=int(-h/10); i<int(h+h/10); i+=h100){
    //hero
    if(int(posHero.y)>=i&&int(posHero.y)<i+h100){drawHero();}
    //enemy
    for(int ii=0; ii<100; ii++){
      if(typeEnemy[ii]!=0){
    if(int(posEnemy[ii].y)>=i&&int(posEnemy[ii].y)<i+h100){drawEnemy(ii);}//if enemy pos y == i
      }//if need draw enemy
    }//for draw enemy
  }//for

What happened - added a picture.
But there are more objects, I need a new code.
I know that sounds silly and hard to understand.
But try, please.

Screenshot_20200322-052244~2

1 Like

can you sort by y-value (or virtual z-value?) and then display in that order?

Chrisir

1 Like

Sorry, but how to sort?
First, write to some sort of array the buffer [] of the object number?
Tell me please.

I figured out how to use sort();
But I don’t understand how to write it into the code.After all, now I have the rest of the variables that are not tied to the desired rendering number yPosEnemy.

1 Like

When you have sorted the figures/ players/ enemies by y value, draw the enemy with the smallest y first, then the figure with the second highest and so forth so that they don‘t cover each other in a wrong way

2 Likes