[Solved] Simpler way to restrict interactions to sprites on the current (visible) layer

I have a ‘Squibble’ object that has two animations (Pink for player 1, and Green for player 2). Other than my base layer, I also have a ‘P1’ layer (where I’ve placed some Squibbles and set them to the Pink animation), and a ‘P2’ layer (where I’ve placed some more Squibbles and set them to the Green animation).

Both Pink (P1) and Green (P2) Squibbles are displayed on the same positions in the game. But only the P1 layer is visible to start with (and therefore only the Pink Squibbles). The P2 layer is hidden (and so are the Green Squibbles).

This is all well and good, except that when I click on a Pink Squibble to ‘activate’ it and make my move as player 1, the Green Squibble on the hidden P2 layer is also activated and moves as well.

How do I restrict interactions (mouse clicks/touches) to only the currently visible layer?

I thought to try this:

image

But that doesn’t work because it still applies to both the Pink and Green Squibble.

I can do this (which works):

image

But it’s convoluted. Is there a simpler way?

I’d love it if this only applied to objects on visible layers, but it only works if the object itself is visible:

image

i.e. the Green Squibbles on the hidden layer are still considered to be ‘visible’, so this doesn’t work.

Use this :

image

I tried that (see my first screenshot above), but it doesn’t seem to work…

Ah, my bad, I somehow missed that image. And I tried with just one object, not two, with one over the other :person_facepalming:

This seems to work :

image

Because there are only 2 objects to iterate over, the repeat is fine to use.

1 Like

Ha, genius! That works nicely, thank you!

EDIT: I see the logic now - because the click/touch picks two Squibbles (on different layers), then you just loop over them to find the one on the visible layer (filtering out the other). Brilliant!