In my xcode project I am trying to create a function that gets called when a SKShapeNode hits the world border(edge of screen) that I have created. The SKShapeNode hits the edge of the screen and rolls to the right due to the gravity but when the contact is made the function didBeginContact isnt called. here is my code
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
struct PhysicsCategory {
static let redBall: UInt32 = 0x1 << 1
static let blueBall: UInt32 = 0x1 << 2
static let worldBorder: UInt32 = 0x1 << 3
}
let slimeBall = SKSpriteNode(imageNamed: "slimeBall")
let lilyPete = SKSpriteNode(imageNamed:"golden")
override func didMoveToView(view: SKView) {
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsWorld.gravity = CGVectorMake(1, 5)
self.physicsBody?.categoryBitMask = PhysicsCategory.worldBorder
self.physicsBody!.node?.name = "world"
self.backgroundColor = UIColor.darkGrayColor()
let playerCircle = SKShapeNode(circleOfRadius: 15)
playerCircle.name = "blueCircle"
playerCircle.fillColor = UIColor.blueColor()
playerCircle.strokeColor = UIColor.blackColor()
playerCircle.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
playerCircle.physicsBody?.categoryBitMask = PhysicsCategory.redBall
playerCircle.physicsBody?.contactTestBitMask = PhysicsCategory.worldBorder | PhysicsCategory.redBall
playerCircle.physicsBody?.collisionBitMask = PhysicsCategory.blueBall | PhysicsCategory.worldBorder
addChild(playerCircle)
let enemyCircle = SKShapeNode(circleOfRadius: 50)
enemyCircle.name = "redCircle"
enemyCircle.fillColor = UIColor.redColor()
enemyCircle.strokeColor = UIColor.blackColor()
enemyCircle.physicsBody = SKPhysicsBody(circleOfRadius: 50)
enemyCircle.position = CGPointMake(self.frame.size.width / 3, self.frame.size.height / 2)
enemyCircle.physicsBody?.affectedByGravity = true
enemyCircle.physicsBody?.dynamic = true
enemyCircle.physicsBody!.mass = 0.05
enemyCircle.physicsBody?.categoryBitMask = PhysicsCategory.blueBall
enemyCircle.physicsBody?.contactTestBitMask = PhysicsCategory.worldBorder | PhysicsCategory.blueBall
enemyCircle.physicsBody?.collisionBitMask = PhysicsCategory.worldBorder | PhysicsCategory.blueBall
addChild(enemyCircle)
enemyCircle.physicsBody?.applyForce(CGVectorMake(-2, -3))
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if(contact.bodyA.node?.name == "redCircle") && (contact.bodyB.node?.name == "world") || (contact.bodyA.node?.name == "world") && (contact.bodyB.node?.name == "redCircle"){
print("Contact Made")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(self)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first! as UITouch
let touchLocation = touch.locationInNode(self)
}
}