[SOLVED] Overlapping sprites / Zorder problem

Hello everyone. I’m new to Gdevelop. I did not find an answer that solves my problem in other posts so… First of all I explain you what I am doing :

I’m trying to do a game in wich “clown sprites” are generated every 3 secs. They have a pattern : they walk to the mid of the screen, then, they walk FRONT (Zorder increasing + scaling the Clown using the ClownY() increasing). When they reach their max Zorder, they switch to “attack” pattern.
All of this works perfectly. Here comes the problem :

You must click on them to kill them (I put the Clowns on a pool outside the camera when they are 0hp). The mechanic works fine when they are not overlapping.

When they overlap, I can’t handle this. How can I solve this ?

In the code below, I tried this. Tt works but overlaping clowns are also clicked and I kill multiple clown by one clic, even they have different Zorder.

Thanks in advance… I hope I’m clear enough.

Second attempt :
To handle the “multiple sprite clicked problem”, I tried to “pick” the nearest clown(Enemy) but it doesn’t pick the nearest Zorder but next to the Mouse XY.

So, this second example is “better” cause it picks a unique sprite, but IT OFTEN PICKS an enemy behind (totally unpredictable)…

Can someone help me ?
It seems Gdevelop seems to rework the “pick nearest” function and add Zorder parameter!
Thanks in advance !

With this, you’ll need a subevent that iterates over all the clowns and determines which has the greatest z-order.

First, you’ll need to set a variable (say called max_clown_z) and set it to an absurdly low figure, like -999.

Then, you’ll need a Repeat for each instance of Clown as a subevent of the first event. Because it is a subevent, it will only repeat over the clown objects that met the conditions in the first event - a filtered list of the clown objects in the scene.

In the actions of that repeat event, set the variable max_clown_z to the max(clown.ZOrder(), Variable(max_clown_z)).

Then finally create another subevent off the first one with conditions Clown.ZOrder() = Variable(max_clown_z) and pick clown nearest the mouse pointer. You now have a clown object with the highest z-order.

However, if you stop increasing the z-order of the clowns once they attach, then you could end up with 2 clown objects have the same z-order and are under the mouse click. In such a case, the clown behind may still get picked. To avoid this, I suggest you keep increasing the z-order while keeping the same Y position and scale.

2 Likes

WOW thanks a lot ! I’ll try it this evening and come back to say if I can do it ! It seems good on the paper :D. Thanks you again.

I did have time to test it now and it works perfectly !!

I must precise I just had to add the RESET of the variable “max_clown_z” to -999 in the end of the second sub event where I put the action to “kill” the clown :wink: because if we don’t refresh, the condition will trigger only with the last highest zorder, even if the new highest zorder is lower :).

WONDERFUL ! Thank you !