1
我在添加和刪除touchesBegan函數中的SKPhysicsJointPin時遇到了問題。問題在於我的聯合聲明是在didMoveToView函數中聲明的,因爲它需要根據其中存在的表達式進行定位。觸摸時添加和刪除SKPhysicsJointPin的問題
因此,我不能在touchesBegan函數中引用SKPhysicsJoint,這對於我正在嘗試執行的項目來說是必需的。任何解決方案或建議?
代碼:
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)
}
}
你可以刪除'touchesBegan'聯合'如果讓合資= head.physicsBody .joints.first {self.physicsWorld.removeJoint(joint)}' – 0x141E