Collision between mult. instances of same sprite

Get help using Construct 2

Post » Tue Feb 19, 2013 4:32 am

Hi,
I have a Sprite called "Ball" and each has an instance variable "Health" (number). When two Balls collide, I want the one with the lower health to be destroyed. However, I'm not sure how to differentiate between the two instances involved in the collision. Here's the best I could come up with:


Here's my understanding of what's going on:
1. When a Ball collides with another Ball, the event is triggered and the two Ball instances are picked for the event.
2. Each Ball's Health variable is compared. I want it to compare it's Health variable to that of the other Ball, but I'm not sure how to specify this.
3. If the Ball's health was lower, it is destroyed; otherwise, nothing happens (once again, I'm not sure how to tell it which of the two Ball's should be destroyed).

I have verified that the collision event is registering, but neither ball is being destroyed when it happens even though one's health is lower than the other. How can I reference specific instances involved in the collision to get this to work? I thought of using "Pick Nth instance," but if I understand correctly that means the Nth out of all instances, not just those in the collision, correct?

Thanks!inejwstine2013-02-19 04:34:38
B
6
Posts: 38
Reputation: 533

Post » Tue Feb 19, 2013 8:12 am

Pick nth instance is exactly what you want. You'd use it like this:

B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Tue Feb 19, 2013 9:12 am

Very interesting, I didn't know you could have event-specific indexes like that :]
I made a capx to try this, but for some reason it doesn't seem to work all the time
Any idea?

PickBall.capx
B
20
S
5
G
8
Posts: 325
Reputation: 6,473

Post » Tue Feb 19, 2013 9:58 am

Actually, the problem is with my understanding of Object(x) and not your code. I was under the impression Object(x) respected the picking process, but it doesn't. So, Ball(0).Health < Ball(1).Health was comparing the balls with IID's of 0 and 1. What needs to happen to make it work right is to store the picked ball's health in a local variable then test that like so:



I probably could make it a little more efficient but it's 2am where I'm at and the brain's fried. :)theubie2013-02-19 10:03:58
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Tue Feb 19, 2013 10:40 am

Ah alright it works now indeed
Nice trick still :]
B
20
S
5
G
8
Posts: 325
Reputation: 6,473

Post » Tue Feb 19, 2013 2:51 pm

Huh, you're right. That works just fine. Like I said above, I thought that "Pick nth index" meant out of all instances in the layout, not just those that are currently picked. Good to know.

A couple other questions:

1. If two separate collisions happen to occur at the exact same time (rare, but not impossible, right?) each will happen in its own separate event, right? Meaning, there won't be four instances to pick from in the event.

2. If three instances all collide together (I don't even know if this one is possible; just want to make sure my bases are covered) then will there be three to pick from? Or would it fire off three separate events: one for the collision between instance 0 and 1, one for 1 and 2, and one for 2 and 0?

Thanks so much for the help! :)
B
6
Posts: 38
Reputation: 533

Post » Tue Feb 19, 2013 8:31 pm

[QUOTE=inejwstine]1. If two separate collisions happen to occur at the exact same time (rare, but not impossible, right?) each will happen in its own separate event, right? Meaning, there won't be four instances to pick from in the event.[/QUOTE]

This is correct. Each collision fire's it's own onCollision event.

[QUOTE=inejwstine]2. If three instances all collide together (I don't even know if this one is possible; just want to make sure my bases are covered) then will there be three to pick from? Or would it fire off three separate events: one for the collision between instance 0 and 1, one for 1 and 2, and one for 2 and 0?[/QUOTE]

If your picker returns 3 objects, then they will be object 0, 1 and 2 (0 based) so you would be correct.
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Wed Feb 20, 2013 2:45 am

1. Thanks!

2. What I meant was, would it be a single event with three instances picked, or three separate events, each with two instances?
B
6
Posts: 38
Reputation: 533

Post » Wed Feb 20, 2013 2:53 am

@inejwstine

There are some events that would return 3 objects. In your specific scenario onCollision returns just two objects.
B
26
S
8
G
3
Posts: 210
Reputation: 5,973

Post » Mon Mar 18, 2013 10:46 pm

Exactly what i was looking for.
Thanks!
B
15
S
4
G
2
Posts: 2
Reputation: 2,338

Next

Return to How do I....?

Who is online

Users browsing this forum: Kagubro and 21 guests