无法获得" didBeginContact"上班

时间:2014-07-30 09:06:59

标签: swift sprite-kit xcode6

我只是在玩swift并且想要在我的任何节点碰撞时执行某些操作。显然我没有那样工作,但我不知道为什么。阅读文档几次,但它主要是为Objective-C编写的,老实说,我认为我不理解它。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

  var eyeset = 0
  var eyes: [SKSpriteNode] = []

  override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
      /* Called when a touch begins */

      for touch: AnyObject in touches {

          let location = touch.locationInNode(self)

          if (eyeset < 9) {

              eyes.insert(SKSpriteNode(imageNamed: "eye"), atIndex: eyeset)
              eyes[eyeset].xScale = 0.25
              eyes[eyeset].yScale = 0.25
              eyes[eyeset].position = location
              eyes[eyeset].physicsBody = SKPhysicsBody(circleOfRadius: eyes[eyeset].size.height/2)
              eyes[eyeset].physicsBody.collisionBitMask = 1
              eyes[eyeset].physicsBody.dynamic = true
              eyes[eyeset].physicsBody.affectedByGravity = true

              self.addChild(eyes[eyeset])
              eyeset++
          }
          else if (eyeset < 10) {

              eyes.insert(SKSpriteNode(imageNamed: "eye"), atIndex: eyeset)
              eyes[eyeset].xScale = 0.5
              eyes[eyeset].yScale = 0.5
              eyes[eyeset].position = location
              eyes[eyeset].physicsBody = SKPhysicsBody(circleOfRadius: eyes[eyeset].size.height/2)
              eyes[eyeset].physicsBody.collisionBitMask = 1
              eyes[eyeset].physicsBody.dynamic = true
              eyes[eyeset].physicsBody.angularDamping = 1
              eyes[eyeset].physicsBody.affectedByGravity = false

              self.addChild(eyes[eyeset])
              eyeset++
          }
          else {
              eyes[eyeset-1].runAction(SKAction.moveTo(location, duration: 0.25))
          }
      }
  }

  func didBeginContact(contact: SKPhysicsContact!) {
      println("Muttermäßig")
  }
}

1 个答案:

答案 0 :(得分:2)

要使联系人检测正常工作,您必须设置contactTestBitMask的{​​{1}}属性。

SKPhysicsBody

eyes[eyeset].physicsBody.contactTestBitMask = 1 指定哪些实体可以在物理环境中相互碰撞,而CollisionBitMask定义物理实体的类别,当节点与它们相交时,联系委托方法将被调用。

阅读文档here