碰撞检测中断另一次碰撞检测 - Swift

时间:2016-06-15 23:30:18

标签: ios xcode swift collision-detection collision

截至目前,我有一个SKSpriteNode可以检测与我的球的碰撞,并在碰撞时将我的得分增加+ 1。同样在碰撞中的代码是检测我的球是否撞到墙壁。

    let scoreNode = SKSpriteNode()

    scoreNode.size = CGSize(width: 750, height: 10)
    scoreNode.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 100)
    scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
    scoreNode.physicsBody?.affectedByGravity = false
    scoreNode.physicsBody?.dynamic = true
    scoreNode.physicsBody?.categoryBitMask = PhysicsCat.Score
    scoreNode.physicsBody?.collisionBitMask = 0
    scoreNode.physicsBody?.contactTestBitMask = PhysicsCat.Ball
    scoreNode.color = SKColor.clearColor()

func didBeginContact(contact: SKPhysicsContact) {


    let firstBody = contact.bodyA
    let secondBody = contact.bodyB



    if firstBody.categoryBitMask == PhysicsCat.Score && secondBody.categoryBitMask == PhysicsCat.Ball{

        score += 1
        scoreLbl.text = "\(score)"
        firstBody.node?.removeFromParent()


    }

    else if firstBody.categoryBitMask == PhysicsCat.Ball && secondBody.categoryBitMask == PhysicsCat.Score {

        score += 1
        scoreLbl.text = "\(score)"
        secondBody.node?.removeFromParent()


    }
    else if firstBody.categoryBitMask == PhysicsCat.Ball && secondBody.categoryBitMask == PhysicsCat.Wall || firstBody.categoryBitMask == PhysicsCat.Wall && secondBody.categoryBitMask == PhysicsCat.Ball{

        enumerateChildNodesWithName("wallPair", usingBlock: ({
            (node, error) in

            node.speed = 0
            self.removeAllActions()

        }))

        if died == false{
            died = true
            createBTN()
            fallDie()
        }
    }

    else if firstBody.categoryBitMask == PhysicsCat.Ball && secondBody.categoryBitMask == PhysicsCat.Wall {

        enumerateChildNodesWithName("wallPair", usingBlock: ({
            (node, error) in

            node.speed = 0
            self.removeAllActions()

        }))
        if died == false{
            died = true
            Ball.physicsBody?.velocity = CGVectorMake(0, 0)

            createBTN()
        }
    }
}

我需要第二次碰撞检测,它完全独立于上面的代码^^。我如何将它们分开?你不能有两个didBeginContact我不认为。我希望这不是太模糊或不清楚。显然我需要第二个节点,称之为colorNode。我如何检测我的球和colorNode之间的碰撞(colorNode比我的scoreNode低约200像素,所以它们不在同一位置),而不会弄乱检测球和墙之间碰撞的代码?

1 个答案:

答案 0 :(得分:0)

您不需要单独的碰撞检测。你需要做的是处理didBeginContact中的事情。所有那些if-else用他们的||检查和&&变得非常笨拙。 (例如,您的上一个else执行的检查已经在之前的else中执行过。)

下面的代码是一个OK-ish尝试使if逻辑更明智。创建Bool以处理我们感兴趣的每种类型的状态。

func didBeginContact(contact: SKPhysicsContact) {
    let firstBody = contact.bodyA
    let secondBody = contact.bodyB

    let ballWasContacted = firstBody.categoryBitMask == PhysicsCat.Ball || secondBody.categoryBitMask == PhysicsCat.Ball
    let wallWasContacted = firstBody.categoryBitMask == PhysicsCat.Wall || secondBody.categoryBitMask == PhysicsCat.Wall
    let scoreWasContacted = firstBody.categoryBitMask == PhysicsCat.Score || secondBody.categoryBitMask == PhysicsCat.Score

现在已经以一种相当合理的方式定义了这个逻辑,你来到实际的碰撞检查,你真的获得了回报:

if ballWasContacted {
    if scoreWasContacted {
        score += 1
        scoreLbl.text = "\(score)"
        let scoreNode = firstBody.categoryBitMask == PhysicsCat.Score ? firstBody.node : secondBody.node
        scoreNode!.removeFromParent()
    } else if wallWasContacted {
        enumerateChildNodesWithName("wallPair", usingBlock: ({
            (node, error) in
            node.speed = 0
            self.removeAllActions()
        }))
        if died == false {
            died = true
            createBTN()
            fallDie()
        }
    }
}

这使得处理另一个球与碰撞事件变得容易得多。您只需要一个PhysicsCat.Color和另一个Bool。然后将另一个else if挂钩到最后一个。

 else if colorWasContacted {
            // Do stuff
        }

注意:遵守惯例也是一个好主意。我在考虑你的命名,不要缩写词。 scoreLabel不是scoreLbl PhysicsCategory而非PhysicsCat *

* PhysicsCat?那是施罗丁格先生的已故宠物吗?