This forum is read only and just serves as an archive. If you have any questions, please post them on

1 decade ago by tunglxx226

I know that both of the questions are asked before but I still can't figure out how to do it, so I've got to post here. Sorry guys :-s

1. Clicked overlapping entities:
So, we know that by default the mouse click will take effect on every entity locates at the place. I found a code snippet here:
However, the code is incomplete, and I fail to fix it. Anyone has any idea how to fix it? Or you have another way to achieve the same task?

2. zIndex:
So I want to use zIndex to sort the entities in my game. I set them to zIndex 1,2,3,4, but nothing change. I tried calling in the draw function() of main but still no hope.

Looking forward to your answer soon.

1 decade ago by mglnunez

For the first problem I'll just give you the jist of what I would do if i only wanted to interact with the topmost entity.

Basically I would check if the mouse is over an entity and if the mouse has been clicked. If it has I would add that entity to an array.
Sort that array by the zIndex.
Get the last entity in the array (assuming ascending order).
Do what ever you want to do with that entity.
Finally empty the array (for the next frame).

For the second problem I'm not exactly sure what is wrong but sorting the entities should be the last thing you do in the update function and that is if you spawned anything in that frame.

1 decade ago by tunglxx226

@mglnunez: thank you very much, I figured out the first problem already :), tks for you help.

The second problem is because I don't really understand how impactjs use zIndex, I tried putting the sorting function to the bottom of the update function, still not changed.

Anw thanks very much :D
Page 1 of 1
« first « previous next › last »