[SOLVED] Collision of same sprite, and Variables

Get help using Construct 2

Post » Sun Nov 13, 2011 2:12 am

I'm stumped on how someone could get a variable from each object on a collision event When both objects are the same sprite. Then use both variables in an action.

practical example would be two drops of water touch and combine into one larger drop.

something like
SpriteA has a variable called "Var1"
SpriteA on collision with SpriteA
     system create SpriteA, set Var1 =SpriteA.Var1+SpriteA.var1
     Destory original twoSpriteA


I searched the forum for an answer with no luck. if something like this has already been asked. I apologize. any help is appreciated. Thanks


            onzephyr2011-11-13 07:14:43
B
84
S
12
G
9
Posts: 99
Reputation: 9,508

Post » Sun Nov 13, 2011 2:44 am

but wouldn't you have to set the Height,Width size to the instance var1...?tonycrew2011-11-13 02:47:33
B
40
S
14
G
11
Posts: 243
Reputation: 9,427

Post » Sun Nov 13, 2011 3:39 am

Yes I would if I decide to make the Resulting SpriteA's size reflect his Var1.   I'm just not sure how to get each of the the original SpriteA's var1 so that I can add them together and set the resulting SpriteA's var1

B
84
S
12
G
9
Posts: 99
Reputation: 9,508

Post » Sun Nov 13, 2011 4:27 am

I'm afraid you will have to wait for famillies for this one.
Having the drops belonging to two famillies (like A and B) and checking for collision between A and B.

That's the "easy way".

After, concerning the "trickier way" it will depends on your game and how you implement it.

If the drops are falling (vertical moving down) drops that are spawned "in line" (not randomly, but one after the other on a progressive X) you might then consider that when two drops collides, they are "neighbours".
Considering a "for each objects" loop runs through the instances in incremential order, you could set a system in which when a

drop is colliding with an other
=> Have a temp variable incremented (for exemple nbDrops)
=> Store the UID of the drop in an array or some global variable (DropOne for example)
=> Store the values that you want (height, width, ...) the same way (an array is advised to store all in one line, you will need two X and as many Y as you have values to store)
Test the value of nbDrops (subevent)
.. System nbDrops = 2
..=> Set nbDrops to 0
..=> Create the new object Drop and set its size by adding the size values contained in your array.
Subevents pick Drop by uid
.... Pick the first UID of the array
....=> Destroy drop
.... Pick the second UID of the array
....=> Destroy drop
.... blank subevent
.... => Clean array


This works on the condition that drops are nearby and that two colliding drops will have close/following UIDs.
If you spawn drops in random positions you might end up with full mayehm of distant drops disappearing and bigger drops appearing widely/randomly on screen.


If someone knows a good way to implement "recognition/separation" in the collision of two instances I'd be glad to hear it (@r0j0hound, @Ashley, ideas ?)
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Sun Nov 13, 2011 4:57 am

No need for families. Use 'pick Nth' in subevents, e.g.:

+ SpriteA collides with SpriteA
----+ Pick SpriteA instance 0
----> (this is the first object in the collision)
----+ Pick SpriteA instance 1
----> (this is the second object in the collision)

In each event you can retrieve the values of the specific instance. Local variables would be handy here - you can store the first instance's variable in a local variable and then use it in the second event, so you can add together each instance's variables or whatever you need.
Scirra Founder
B
359
S
214
G
72
Posts: 22,949
Reputation: 178,544

Post » Sun Nov 13, 2011 5:37 am

Oh very nice.
Thanks a lot Ash !
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,998
Reputation: 57,791

Post » Sun Nov 13, 2011 7:13 am

Thank you Ashley that's exactly what I was looking for. And now I know what Pick Nth does. Linked is a fast example of the answer. it works but seems to have a flaw in it that pops up if I don't reset a variable on the sprite "drop" So caution, it still needs tweaking.

link to example   .Capx
B
84
S
12
G
9
Posts: 99
Reputation: 9,508


Return to How do I....?

Who is online

Users browsing this forum: benajad, Braus, R0J0hound, shinkan and 24 guests