I'm having trouble with adding and removing my SKPhysicsJointPin in the touchesBegan function. The issue is that my joint is declared in the didMoveToView function because it needs to be positioned based on expressions that exists within it.
That being so I can't refer to the SKPhysicsJoint within the touchesBegan function, which is necessary for what I'm trying to do with my project. Any solutions or suggestions?
Code:
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var head = SKSpriteNode(imageNamed: "crown.png")
var headTexture = SKTexture(imageNamed: "crown.png")
var neck = SKSpriteNode(imageNamed: "throat.png")
var neckTexture = SKTexture(imageNamed: "throat.png")
override func didMoveToView(view: SKView) {
head.position.x = torso.position.x - 1
head.position.y = torso.position.y + 77
head.physicsBody = SKPhysicsBody(texture: headTexture, size: head.size)
head.physicsBody!.categoryBitMask = ColliderType.part.rawValue
head.physicsBody!.contactTestBitMask = ColliderType.part.rawValue
head.physicsBody!.collisionBitMask = ColliderType.part.rawValue
self.addChild(head)
neck.position.x = torso.position.x
neck.position.y = torso.position.y + 44
neck.physicsBody = SKPhysicsBody(texture: neckTexture, size: neck.size)
neck.physicsBody!.categoryBitMask = ColliderType.mjoint.rawValue
neck.physicsBody!.contactTestBitMask = ColliderType.mjoint.rawValue
neck.physicsBody!.collisionBitMask = ColliderType.mjoint.rawValue
self.addChild(neck)
let headToNeck = SKPhysicsJointPin.jointWithBodyA(head.physicsBody!, bodyB:neck.physicsBody!, anchor:CGPointMake(head.position.x, head.position.y - 20.8))
headToNeck.shouldEnableLimits = true
self.physicsWorld.addJoint(headToNeck)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.physicsWorld.removeJoint(headToNeck)
}
}