Why are didBeginContact called multiple times?
I had the same problem (score increasing multiple times for a single enemy destroyed and multiple life points being lost for a single instance of damage.) A user on the Apple forums thinks that it's a bug in [SKPhysicsBody bodyWithTexture:size:] but I don't believe that's the case, because it was happening with other constructors too.
First off, the categoryBitMask
and contactTestBitMask
are very important, obviously. Take a look at Apple's SpriteKit Physics Collisions sample code:
// Contacts are often a double dispatch problem; the effect you want is based on the type of both bodies in the contact. This sample this in a brute force way, by checking the types of each. A more complicated example might use methods on objects to perform the type checking.
// The contacts can appear in either order, and so normally you'd need to check
each against the other. In this example, the category types are well ordered, so
the code swaps the two bodies if they are out of order. This allows the code
to only test collisions once.
What I did to solve it was setting a flag after handling each condition. In my case, I was testing whether bodyA.node.parent
was nil in didBeginContact
, because I called removeFromParent()
on the missile/enemy nodes to destroy them.
I think you should expect the event to fire multiple times and your code in there has to make sure it's processed only once.
Why are didBeginContact called multiple times?
I had the same problem (score increasing multiple times for a single enemy destroyed and multiple life points being lost for a single instance of damage.) A user on the Apple forums thinks that it's a bug in [SKPhysicsBody bodyWithTexture:size:] but I don't believe that's the case, because it was happening with other constructors too.
First off, the categoryBitMask
and contactTestBitMask
are very important, obviously. Take a look at Apple's SpriteKit Physics Collisions sample code:
// Contacts are often a double dispatch problem; the effect you want is based on the type of both bodies in the contact. This sample this in a brute force way, by checking the types of each. A more complicated example might use methods on objects to perform the type checking.
// The contacts can appear in either order, and so normally you'd need to check
each against the other. In this example, the category types are well ordered, so
the code swaps the two bodies if they are out of order. This allows the code
to only test collisions once.
What I did to solve it was setting a flag after handling each condition. In my case, I was testing whether bodyA.node.parent
was nil in didBeginContact
, because I called removeFromParent()
on the missile/enemy nodes to destroy them.
I think you should expect the event to fire multiple times and your code in there has to make sure it's processed only once.
Related Topics
How to Scroll to the Bottom of a Uitableview on the iPhone Before the View Appears
How to Correctly Handle Weak Self in Swift Blocks with Arguments
Uicollectionview: Animate Cell Size Change on Selection
My Prerelease App Has Been "Processing" for Over a Week in Itunes Connect, What Gives
Prevent Segue in Prepareforsegue Method
Setting Direction for Uiswipegesturerecognizer
Xcode 4.2 How Include One Project into Another One
Invalid Swift Support - Files Don't Match
How to Convert a String to an Md5 Hash in iOS Using Swift
Present Modal View Controller in Half Size Parent Controller
How to Make Uitextview Detect Links for Website, Mail and Phone Number
Uitableview Mix of Static and Dynamic Cells
No Umbrella Header Found for Target, Module Map Will Not Be Generated
How to Pass Multiple Values with a Notification in Swift