Collision between mult. instances of same sprite

  • 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:

    <img src="https://dl.dropbox.com/u/32626868/Pictures/Collision.png" border="0" />

    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!

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

    <img src="http://www.infinitepossibilitygames.com/demos/MultipleInstance/MultipleInstanceCollide.png" border="0" />

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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:

    <img src="http://www.infinitepossibilitygames.com/demos/MultipleInstance/MultipleInstanceCollide2.PNG" border="0" />

    I probably could make it a little more efficient but it's 2am where I'm at and the brain's fried. :)

  • Ah alright it works now indeed

    Nice trick still :]

  • 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! :)

  • 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.

    This is correct. Each collision fire's it's own onCollision 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?

    If your picker returns 3 objects, then they will be object 0, 1 and 2 (0 based) so you would be correct.

  • 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?

  • inejwstine

    There are some events that would return 3 objects. In your specific scenario onCollision returns just two objects.

  • Exactly what i was looking for.

    Thanks! <img src="smileys/smiley4.gif" border="0" align="middle" />

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)