Search code examples
swiftsprite-kitskphysicscontact

Best way to "eat" something in SpriteKit


By "eat" I mean: when sprite A (Mario) collides with sprite B (a coin) the collision is detected and the coin is removed from the scene; however, Mario's movement is not altered by the collision with the coin.

At the moment I'm using SKPhysicsContactDelegate to register when Mario and the coin collide, but this seems to require acknowledging the coin as a physical body - which therefore means that Mario's movement is stopped by it.

Should the coin not have a physics body, and instead should I use a different method to see if they contact?


Solution

  • According to Apple...

    var categoryBitMask: UInt32 A mask that defines which categories this physics body belongs to.

    var collisionBitMask: UInt32 A mask that defines which categories of physics bodies can collide with this physics body.

    var contactTestBitMask: UInt32 A mask that defines which categories of bodies cause intersection notifications with this physics body.

    So if you just set the contactTestBitMask on "Mario" to the coin categoryBitMask, and set the collisionBitMask on "Mario" to 0 (or not to the coin categoryBitMask) you should get be able to tell when the 2 collide in the didBegin(_ contact: SKPhysicsContact) func but the coin will not impede Mario's movement