2016-07-06 3 views
1

touchesBegan関数でSKPhysicsJointPinを追加および削除する際に問題があります。問題は、ジョイントがdidMoveToView関数内に存在する式に基づいて配置される必要があるため、ジョイントが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) 

    } 
} 
+0

共同= head.physicsBody .joints.firstせている場合は、 ''でtouchesBegan'での共同を削除することができますか? {self.physicsWorld.removeJoint(joint)} ' – 0x141E

答えて

0

すると、このようなクラス変数を作成してみてください:

class GameScene: SKScene, SKPhysicsContactDelegate { 

     var headToNeck: SKPhysicsJoint! 
     //other variables 

     override func didMoveToView(view: SKView) { 
      //other code 
      self.headToNeck = SKPhysicsJointPin.jointWithBodyA(head.physicsBody!, bodyB:neck.physicsBody!, anchor:CGPointMake(head.position.x, head.position.y - 20.8)) 
      self.headToNeck.shouldEnableLimits = true 
      self.physicsWorld.addJoint(self.headToNeck) 
     } 

     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
      self.physicsWorld.removeJoint(self.headToNeck) 
     } 
    } 
+0

私は昨日このソリューションを見つけましたが、実際にはこの回答に非常によく似ていました。 –

関連する問題