didBeginContact not being called Swift
As mentioned above it seems you have misspelled your node names. You are looking for "enemy" in the collision method but named your enemies "enemyBall". Thats why we should always create constants to avoid this e.g.
let enemyName = "Enemy"
and than use it like so
enemy.name = enemyName
You can also try writing your collision method like this, which should make it slightly nicer to read and you only need 1 if statement per collision. Also this way you do not need node names to compare bodies.
func didBegin(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// Main ball with enemy or enemy with main ball
if (firstBody.categoryBitMask == bitMasks.mainBall) && (secondBody.categoryBitMask == bitMasks.enemy) {
// do something
}
}
I would also try to follow the swift guidlines, classes, enums and structs should start with capital letters.
Hope this helps
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.
didBeginContact function not being called
Your definition for didBeginContact() is buried in the touchesEnded() function. If you straighten out your bracing, it might help:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
// for touches loop (skipped)
}
} // Add this closing brace
func didBeginContact(contact: SKPhysicsContact) {
// contact body (skipped)
}
And you'll also need to delete the extra brace after update():
// ...skipped
func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
// } // get rid of this one
}
Hope that helps.
didBeginContact not called
From the docs,
An object that implements the SKPhysicsContactDelegate protocol can respond when two physics bodies with overlapping
contactTestBitMask values are in contact with each other in a physics
world. To receive contact messages, you set the contactDelegate
property of a SKPhysicsWorld object. The delegate is called when a
contact starts or ends.
Consequently, you'll need to adopt the SKPhysicsContactDelegate
protocol and set the physicsWorld
's contactDelegate
property to the SKScene by
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
}
}
Also, you should remove the ? from the following statements
player.planeSprite.physicsBody? = SKPhysicsBody(rectangleOfSize: player.planeSprite.size)
note.physicsBody? = SKPhysicsBody(rectangleOfSize: note.size)
didBeginContact not being called
You need to declare yourself as the contact delegate of your physics world:
// add conformance to SKPhysicsContactDelegate:
class GameScene: SKScene, SKPhysicsContactDelegate {
// ...
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
// set as delegate:
self.physicsWorld.contactDelegate = self
// ..
}
// should be called now
func didBeginContact(contact: SKPhysicsContact){
println("colliding!")
}
}
SpriteKit's didBeginContact can't be called
Find out the reason. Need use
s1.physicsBody!.dynamic = true
s2.physicsBody!.dynamic = true
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];
Swift: didBeginContact() is not called after reloading level
Eventually I figured it out. I don't know why, but after replaying the game and reloading everything, the order in which collisions occur is reversed. For each collision statement, I had to change....
contact.bodyA.categoryBitMask == playerGroup && contact.bodyB.categoryBitMask == platformGroup
To....
contact.bodyA.categoryBitMask == playerGroup && contact.bodyB.categoryBitMask == platformGroup || contact.bodyA.categoryBitMask == platformGroup && contact.bodyB.categoryBitMask == playerGroup
If someone could clarify why this change needed to be implemented, feel free to comment below.
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
Related Topics
How to Draw Line Node Keep Same Size in Camera as Measure App in Iphone
Captureoutput Function Isn't Called Using Setsamplebufferdelegate
Swiftui MACos Scroll a List with Arrow Keys While a Textfield Is Active
iOS Swift Didbegincontact Not Being Called
Swiftui MACos Commands (Menu Bar) and View
Apple Vision - Barcode Detection Doesn't Work for Barcodes with Different Colours
Add Skin Tone Modifier to an Emoji Programmatically
Macos App Local Notification Not Showing When Testing with Xcode
Swift Probability of Random Number Being Selected
Appending Text to Nstextview in Swift 3
Swift 3/Macos: Open Window on Certain Screen
How to Create Objects from Swiftyjson
Uisearchbar's Set_Cancelbuttontext: Ivar Is Prohibited
Swift 3:Appdelegate Does Not Conform to Protocol Gidsignindelegate
Swift Create Byte Buffer Holder for Nsstream
Margin Between Images in Uiscrollview
Swift: Make Translucent Overlapping Lines of the Same Color Not Change Color When Intersecting