Reputation: 791
I created a struct PhysicsCatagory for each of the different objects I want interacting with each other
struct PhysicsCatagory {
static let Blade : UInt32 = 1
static let Laser : UInt32 = 2
}
above my class GameScene
class GameScene: SKScene, SKPhysicsContactDelegate {
and I initialized an SKSpriteNode blade with its physicsBody in the didMoveToView method
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
Blade.position = CGPointMake(self.size.width / 2, (self.size.height / 14))
Blade.anchorPoint = CGPointMake(0.5, -0.13)
Blade.physicsBody = SKPhysicsBody(rectangleOfSize: Blade.size)
Blade.physicsBody?.categoryBitMask = PhysicsCatagory.Blade
Blade.physicsBody?.contactTestBitMask = PhysicsCatagory.Laser
Blade.physicsBody?.dynamic = false
self.addChild(Blade)
}
As well as a SKSpriteNode laser and its physicsbody in the method shootLaser
func shootLaser(){
var Laser = SKSpriteNode(imageNamed: "Laser.png")
Laser.position = Enemy.position
Laser.zPosition = -5
Laser.physicsBody = SKPhysicsBody(rectangleOfSize: Laser.size)
Laser.physicsBody?.categoryBitMask = PhysicsCatagory.Laser
Laser.physicsBody?.contactTestBitMask = PhysicsCatagory.Blade
Laser.physicsBody?.dynamic = false
let action = SKAction.moveBy(laserVector, duration: 0.7)
let actionDone = SKAction.removeFromParent()
Laser.runAction(SKAction.sequence([action,actionDone]))
self.addChild(Laser)
}
But when they collide in the simulation, the didBeginContact method is not called and "Hello" is not printed
func didBeginContact(contact: SKPhysicsContact) {
NSLog("Hello")
}
Why isn't the didBeginContact method being called when they collide? Thanks in advance (:
Upvotes: 2
Views: 339
Reputation: 12753
Sprite Kit does not check for contacts between non-dynamic physics bodies because they aren't expected to move. If you don't want your sprites to fall off the screen due to gravity, set the physics body's affectedByGravity
property to false
and set dynamic = true
.
Upvotes: 3