在Swift 2中检测与节点的子节点和另一个节点的冲突

时间:2016-02-15 07:49:56

标签: ios swift sprite-kit

所以我创建了一个名为planet的父节点,它有两个孩子,其中一个是登陆桌面。我试图检测这个孩子与名为“着陆器”的另一个节点之间的冲突。我尝试过很多选择,但似乎没什么用。提前谢谢。

  func createPlanet() {
    var planet = SKSpriteNode()
    planet.zPosition = 1
    planet.name = "mars"

    redPlanet = SKSpriteNode(imageNamed: "redPlanet")
    redPlanet.name = "red"
    redPlanet.zPosition = 2
    planet.addChild(redPlanet)

    landingPad = SKSpriteNode(imageNamed: "landingPad")
    landingPad.name = "pad"
    landingPad.zPosition = 3
    landingPad.position = CGPoint(x: 0, y: redPlanet.size.height / 2 - 60)
    landingPad.physicsBody = SKPhysicsBody(texture: landingPad.texture!, size: size)
    landingPad.physicsBody!.dynamic = false
    landingPad.physicsBody!.categoryBitMask = landingPadMask
    landingPad.physicsBody!.collisionBitMask = landerMask
    landingPad.physicsBody!.contactTestBitMask = 0
    planet.addChild(landingPad)

    planet.position = CGPoint(x: frame.size.width / 2, y: -redPlanet.size.height / 6)
    planet.physicsBody = SKPhysicsBody(circleOfRadius: redPlanet.size.height / 2)
    planet.physicsBody!.dynamic = false
    planet.physicsBody!.categoryBitMask = planetMask
    planet.physicsBody!.contactTestBitMask = 0

    let spinner = SKAction.rotateByAngle(CGFloat(M_PI), duration: 3)
    planet.runAction(SKAction.repeatActionForever(spinner))

    addChild(planet)
  }

着陆器代码......

  func createLander() {
    var randomX = RandomInt(min: 10, max: 400)

    lander = SKSpriteNode(imageNamed: "lander")
    lander.position = CGPoint(x: CGFloat(randomX), y: frame.size.height + 20)
    lander.name = "lander"
    lander.zPosition = 10


    lander.physicsBody = SKPhysicsBody(texture: lander.texture!, size: lander.size)
    lander.physicsBody!.dynamic = true
    lander.physicsBody!.affectedByGravity = true
    lander.physicsBody!.friction = 0.2
    lander.physicsBody!.restitution = 0.75
    lander.physicsBody!.linearDamping = 0.208
    lander.physicsBody!.angularDamping = 0.1
    lander.physicsBody!.categoryBitMask = landerMask
    lander.physicsBody!.collisionBitMask = landingPadMask | cometMask

    addChild(lander)
  }

1 个答案:

答案 0 :(得分:0)

你错过了:

lander.physicsBody!.contactTestBitMask = landingPadMask | cometMask

landingPad.physicsBody!.contactTestBitMask = landerMask

代表:

self.physicsWorld.contactDelegate = self; 

然后你得到了碰撞:

didBeginContactdidEndContact