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.
iOS Swift didBeginContact not being called
It turned out to be a simple problem. In my original code I was setting parameters for the SKPhysicsBody detection frame like so:
let carBody = SKPhysicsBody(
rectangleOfSize: car.frame.size, center: car.position)
Similarly I was doing the same for the second node that I was testing physics collisions for.
Simply removing the 'centre:' parameters like so:
let carBody = SKPhysicsBody(rectangleOfSize: car.frame.size)
for the two sprite nodes solved the problem and the nodes now crash into each other and push themselves aside as expected.
Swift spritekit didbegincontact being called with delay
Your problem isn't things are being delayed, your problem is your bounding box is not where you think it is
use view.showPhysics = true
to determine where your boxes are
once you realize they are in the wrong spots, go to this line
knight.physicsBody = SKPhysicsBody(rectangleOf: knight.size, center: knight.position)
and fix it
knight.physicsBody = SKPhysicsBody(rectangleOf: knight.size)
Do so for the rest of your bodies
SpriteKit didBeginContact not being called
You haven't added your physics body for coins
SKSpriteNode *coins = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(4, 4)];
coins.name = @"coins";
coins.position = CGPointMake(self.coinX+80, ground.position.y + ground.frame.size.height/2 + obstacle.frame.size.height/2 + 25);
coins.physicsBody = [SKPhysicsBody bodyWith...//need code here
coins.physicsBody.categoryBitMask = coinCategory;
coins.physicsBody.dynamic = NO;
coins.physicsBody.collisionBitMask = 0;
SKAction *revolution = [SKAction rotateByAngle:M_PI_4*10 duration:3];
SKAction *repeatRotate = [SKAction repeatActionForever:revolution];
[coins runAction:repeatRotate];
[self.world addChild:coins];
Related Topics
Swift - How to Open Specific View Controller When Push Notification Received
Accessing Objective-C Base Class's Instance Variables from a Swift Class
Crashlytics Does Not Show Crashes
Nsindexpath? Does Not Have a Member Name 'Row' Error in Swift
Uialertcontroller Set Attributed Messages
Animation Blocks Resets to Original Position After Updating Text
String Replace Phone Number iOS Swift
iOS Library to Convert Powerpoint Presentations into Images
Handing Firebase + Facebook Login Process
Show Data in Uipickerview Second Component Based on First Component Selection
How to Switch to Speaker Output When Bluetooth Headsets Are Connected
Replace iOS App Emoji with Twitter Open Source Twemoji
How to Use Avcapturephotooutput to Capture Photo Swift + Xcode
How to Have Firebase Automatically Delete Values Older Than 30 Minutes
Rails: Redirect_To 'Myapp://' to Call iOS App from Mobile Safari
How to Access User Defined Runtime Attribute from the 'Sender' Object