How do I select just the top sprite?:

Get help using Construct 2

Post » Thu Sep 15, 2016 10:23 am

Two sprites overlap each other. If I click on one of them sprite specific actions happen. Problem: If I click in an area where both sprites overlap the action of the bottom sprite is performed.
Possible solution: Create an array. All touched sprites will be listed in one column and their layer in another column. Now sort the list by layer and choose the object with the highest layer. Now perform just the actions for that object.
Are there any better solutions? If no, how would I create such an event in CS2?
B
3
Posts: 4
Reputation: 533

Post » Thu Sep 15, 2016 10:35 am

You don't need to do anything as complicated as that, when two sprites overlap you can reference them as one being 0 and the other being 1. Look at ashley's response here about picking viewtopic.php?t=86825&start=0
B
46
S
16
G
74
Posts: 2,089
Reputation: 44,187

Post » Thu Sep 15, 2016 10:51 am

Or even simpler add the common condition "Pick top instance" from the "Z Order" category which is specifically dedicated to this kind of issue.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
289
S
112
G
94
Posts: 7,333
Reputation: 69,293

Post » Thu Sep 15, 2016 12:08 pm

In a scenario like the one below. How would I solve the problem?
On left mouse click resize the sprite.
The event sheet:
Image
Layout:
Image

If I click in between the objects both of them resize but I just want the green box to get bigger and not the pink one to get smaller.
B
3
Posts: 4
Reputation: 533

Post » Thu Sep 15, 2016 4:34 pm

You can do this with one event I think. Add your objects to a family.

On left button clicked on Family
Condition: Pick top instance > Family Set scale
B
46
S
16
G
74
Posts: 2,089
Reputation: 44,187

Post » Thu Sep 15, 2016 5:23 pm

As I couldn't find a way to resolve this issue by using a Mouse event, the issue was resolved by using a Touch event instead. :mrgreen:

What was needed

1. When more than two stacked objects are clicked, only the top sprite's event must be triggered.

Solution

1. On touched (Object1).
2. (Object1) - Pick top instance.
3. (Object1) - Scale 1.5.


Then I had to make sure a stacked object located below was also 'tappable' with its own event.

1. On touched (Object2).
2. If (Object1) is not touched, scale (Object2) 0.5.


So far, this was the only solution that was able to find, after more than an hour of research.

At first, it looked very simple to resolve, but it was a difficult one.. I've also spent hours on the same issue in the past, but I couldn't find the answer at that time.

Check out my example file. :mrgreen:

https://www.dropbox.com/s/qte9ubnfou1um ... .capx?dl=0
B
11
S
6
G
15
Posts: 256
Reputation: 9,207

Post » Thu Sep 15, 2016 8:01 pm

That method gets crazy out of hand with more than 2 sprites, you'd have to put If Object 1 is not touched, if Object 2 is not touched etc infinite amounts of conditions for every overlap check with different object types, plus that still wouldn't work with multiple instances of the same object, it assumes SpriteA is always on top and then SpriteB etc as it checks them in order. My above method handles any number of overlapped objects with one event.
B
46
S
16
G
74
Posts: 2,089
Reputation: 44,187

Post » Thu Sep 15, 2016 9:20 pm

plinkie wrote:That method gets crazy out of hand with more than 2 sprites, you'd have to put If Object 1 is not touched, if Object 2 is not touched etc infinite amounts of conditions for every overlap check with different object types, plus that still wouldn't work with multiple instances of the same object, it assumes SpriteA is always on top and then SpriteB etc as it checks them in order. My above method handles any number of overlapped objects with one event.


Yes, but I have found the solution as per punktum's request.

1. Two sprites.
2. Click an area that is overlapping.
3. Only the top sprite's event is triggered.

I've also experimented with the Families option, hence the 'Family1' object in my example file, but decided not to venture into that field any further after spending too much time on a new issue.

The issue I encountered was trying to trigger two different event actions (Green = scale x 1.5 & Blue = scale x 0.5) as Families, so that option required more than one set of extra code to allow the Blue to be picked as 'Pick top instance'.

When I encountered a similar issue in the past, I ended up spending days trying to find an answer without success.. so after spending slightly over an hour earlier today, I've decided to provide a solution on what punktum initially asked and step off; as that's better than giving him theological instructions without providing an actual example file on what to do on his own.

This issue appears to be simple to resolve, but it's not.

If anyone else could take over the coding, that would be great, as I want to learn too. :mrgreen:

I have a feeling that an easy solution will be found with functions and arrays that I'm not fond of. :(
B
11
S
6
G
15
Posts: 256
Reputation: 9,207

Post » Wed Sep 21, 2016 10:18 pm

The answer is out. Check out 99Instances2Go's post below. :D

viewtopic.php?f=147&t=182129&p=1069536#p1069536
B
11
S
6
G
15
Posts: 256
Reputation: 9,207

Post » Wed Sep 21, 2016 10:23 pm

That's exactly the same as what I posted above. Click on Family > Pick top instance. The non-family .capx is pretty much what you posted but like I said it gets out of hand with the comparisons on loads of objects.
B
46
S
16
G
74
Posts: 2,089
Reputation: 44,187

Next

Return to How do I....?

Who is online

Users browsing this forum: brunopalermo, jorgmaquoi, zenox98 and 26 guests