Search code examples
iosswiftsprite-kitcollision-detectioncollision

Detect Contact After Changing contactTestBitMask in SpriteKit


I'm currently creating a 2D game in Swift using SpriteKit and I'm having a problem with the collision detection.

Let's say I have SKSpriteNode Foo in the game. Then let's say SKSpriteNode Bar spawns on top of Foo, but does NOT have the contactTestBitMask to trigger a contact. Then after a short moment (let's say 1 second) Bar changes its contactTestBitMask to detect contact with Foo.

Currently, Foo will have to actually move around a little bit OR move out from under Bar and return back to it to detect contact with it. I need Bar to immediately trigger contact with Foo when it changes its contactTestBitMask.

Maybe there's a different way to do this without changing the contactTestBitMask?

Thanks!


Solution

  • If you create a physics body object intersecting another physics body object, no collision will register. It has something to do with the way SK registers a collision through movement only.

    You have the option of using intersectsNode: when creating your object to see if it intersects another object and handle any subsequent code accordingly.