碰撞检测不完全

时间:2017-09-03 18:56:48

标签: ios swift sprite-kit

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    let balls = [
    SKSpriteNode(imageNamed: "blueball.png"),
    SKSpriteNode(imageNamed: "greenball.png"),
    SKSpriteNode(imageNamed: "realredball.png"),
    ]
let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png")
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png")
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png")

let blueBallCategory :UInt32 = 0x1 << 0
let greenBallCategory :UInt32 = 0x1 << 1
let realRedBallCategory :UInt32 = 0x1 << 2
let redRectangleCategory : UInt32 = 0x1 << 3
let blueRectangleCategory : UInt32 = 0x1 << 4
let greenRectangleCategory : UInt32 = 0x1 << 5

override func didMove(to view: SKView) {
    spawnBallsandRectangles()
    physicsWorld.contactDelegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    physics()
}
func didBegin(_ contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }
    if firstBody.categoryBitMask == blueBallCategory && secondBody.categoryBitMask == blueRectangleCategory {
        print("alive")
        }
    else if firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == greenRectangleCategory {
        print("alive")
    }
    else if firstBody.categoryBitMask == realRedBallCategory && secondBody.categoryBitMask == redRectangleCategory {
        print("alive")
    }
    else if firstBody.categoryBitMask == blueBallCategory || firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == redRectangleCategory{
        print("dead")
    }
    else if firstBody.categoryBitMask == realRedBallCategory || firstBody.categoryBitMask == greenBallCategory && secondBody.categoryBitMask == blueRectangleCategory {
        print("dead")
    }
    else if firstBody.categoryBitMask == realRedBallCategory || firstBody.categoryBitMask == blueBallCategory && secondBody.categoryBitMask == greenBallCategory {
    print("dead")
        }
    }


func spawnBallsandRectangles() {
    let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
    ball.position = CGPoint(x: -200, y: 250)
    ball.size = CGSize(width: 70, height: 70)
    redRectangle.position = CGPoint(x: 0, y: -600)
    redRectangle.size = CGSize(width: 200, height: 20)
    blueRectangle.position = CGPoint(x: -200, y: -600)
    blueRectangle.size = CGSize(width: 200, height: 20)
    greenRectangle.position = CGPoint(x: 200, y: -600)
    greenRectangle.size = CGSize(width: 200, height: 20)
    self.addChild(ball)
    self.addChild(redRectangle)
    self.addChild(blueRectangle)
    self.addChild(greenRectangle)

}
func physics(){
    for ball in balls{
        ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
               }
    redRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
    redRectangle.physicsBody?.affectedByGravity = false
    redRectangle.physicsBody?.isDynamic = false
    blueRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
    blueRectangle.physicsBody?.affectedByGravity = false
    blueRectangle.physicsBody?.isDynamic = false
    greenRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
    greenRectangle.physicsBody?.isDynamic = false
    greenRectangle.physicsBody?.affectedByGravity = false
    balls[0].physicsBody?.categoryBitMask = blueBallCategory
    balls[1].physicsBody?.categoryBitMask = greenBallCategory
    balls[2].physicsBody?.categoryBitMask = realRedBallCategory
    balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
    balls[1].physicsBody?.contactTestBitMask = redRectangleCategory
    balls[1].physicsBody?.contactTestBitMask = blueRectangleCategory
    balls[2].physicsBody?.contactTestBitMask = blueRectangleCategory
    balls[0].physicsBody?.contactTestBitMask = greenRectangleCategory
    balls[2].physicsBody?.contactTestBitMask = greenRectangleCategory
    balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
    balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
    balls[2].physicsBody?.contactTestBitMask = redRectangleCategory
    redRectangle.physicsBody?.categoryBitMask = redRectangleCategory
    blueRectangle.physicsBody?.categoryBitMask = blueRectangleCategory
    greenRectangle.physicsBody?.categoryBitMask = greenRectangleCategory


}
}

我已经设置了碰撞检测,这样如果一个球碰撞到一个匹配颜色的矩形,程序就会打印出来(&#34; alive&#34;)。如果颜色不匹配,程序应打印(&#34;死&#34;)。它会检测碰撞,如果颜色匹配并打印(&#34;活着&#34;)但它不打印(&#34;死&#34;)。有人可以帮忙,谢谢。

1 个答案:

答案 0 :(得分:1)

你可能会发现像这样构建你的didBegin()可能有助于摆脱所有else if

func didBegin(_ contact: SKPhysicsContact) {

    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contactMask {

    case blueBallCategory | blueRectangleCategory:
       print("Alive! Blue ball has hit blue rectangle.")

    case greenBallCategory | greenRectangleCategory:
       print("Alive! Green ball has hit green rectangle.")

    case redBallCategory | redRectangleCategory:
       print("Alive! Red ball has hit red rectangle.")

    default :
        print("Dead! Some other contact has occurred")
    }
}

如果球击中不同颜色的矩形,你想要发生什么?

修改

通过physics()函数中的此代码:

balls[0].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = redRectangleCategory
balls[1].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[2].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[0].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory

每次为特定物理主体的contactTestbitMask分配新值时,您将覆盖之前的值,而不是添加它。因此,唯一重要的是:

balls[0].physicsBody?.contactTestBitMask = blueRectangleCategory
balls[1].physicsBody?.contactTestBitMask = greenRectangleCategory
balls[2].physicsBody?.contactTestBitMask = redRectangleCategory

因此球[0](蓝色球)仅接触蓝色矩形,球[1](绿色球)仅接触绿色矩形,球[2](真红色球)仅接触红色矩形。因此,如果球接触到不同颜色的矩形,则不会调用didBegin(),这就是为什么你永远不会看到你的“死”信息。

另一个编辑:

如果你想让所有球在接触到任何矩形时触发didBegin(),那么你的球contactTestBitMasks应该包括所有矩形:

for ball in balls {
    balls.physicsBody?.contactTestBitMask = blueRectangleCategory | greenRectangleCategory | redRectangleCategory
}

或者如果球已经定义了一些触点,你只想将矩形添加到它们的碰撞位掩码中:

for ball in balls {
    balls.physicsBody?.contactTestBitMask |= (blueRectangleCategory | greenRectangleCategory | redRectangleCategory)
}