Search code examples
iosobjective-csprite-kitgame-physicsskphysicsbody

How to determine which physics object ended contact in sprite kit


I've been trying to call a method in the method didEndContact:contact when a player jumps off a certain physics object called "triFloor", how would I run a method in didEndContact:contact only when the players contact with "trifFloor" ends?


Solution

  • You don't run didEndContact: just between player and trifFoor (unless those are the only two with physics body), you run the method every time and in the method, find who it made contact with and do the appropriate actions.

    - (void)didEndContact:(SKPhysicsContact *)contact {
    
        if ((contact.bodyA == player && contact.bodyB == trifFoor) ||
            (contact.bodyA == trifFoor && contact.bodyB == player)) {
            //Do what you want here.
        }
    }