我正在制作一个游戏,我将一个球创建为SKSpriteNode,它与我不想要的一些PhysicsBody碰撞。这是我的球:
orangeBall = OrangeBall(path: ballTrajectory, color: UIColor.orange, borderColor: UIColor.black)
orangeBall.physicsBody = SKPhysicsBody(circleOfRadius: Physics.PhysicsNumbers.ballRadius)
orangeBall.position = Physics.PhysicsNumbers.ballRestPos
orangeBall.physicsBody?.categoryBitMask = CollisionsChecker.Ball
orangeBall.physicsBody?.collisionBitMask = CollisionsChecker.Frame | CollisionsChecker.Box | CollisionsChecker.Score
orangeBall.physicsBody?.contactTestBitMask = CollisionsChecker.Frame | CollisionsChecker.Box | CollisionsChecker.Score
orangeBall.physicsBody?.isDynamic = false
orangeBall.physicsBody?.affectedByGravity = false
addChild(orangeBall)
我希望它能够检测到它已经与这个得分物理体接触但实际上没有发生碰撞:
score = NetBoxes(path: scoreStationary, color: UIColor.red, borderColor: UIColor.red)
score.position = Physics.PhysicsNumbers.scoreRest
score.physicsBody = SKPhysicsBody(circleOfRadius: 50)
score.physicsBody?.categoryBitMask = CollisionsChecker.Score
score.physicsBody?.contactTestBitMask = 0
score.physicsBody?.collisionBitMask = CollisionsChecker.Ball
score.physicsBody?.affectedByGravity = false
score.physicsBody?.isDynamic = false
addChild(score)
(circleOfRadius仅用于测试目的)每次我尝试检查我的碰撞时,它似乎都没有输出我想要它做的事情:
func didBegin(_ contact: SKPhysicsContact){
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == CollisionsChecker.Ball && secondBody.categoryBitMask == CollisionsChecker.Score || firstBody.categoryBitMask == CollisionsChecker.Score && secondBody.categoryBitMask == CollisionsChecker.Ball{
print("Ball in hoop")
}
基本上,我的orangeBall是一个SKSphapeNode,总是与我的Score PhysicsBody相撞。也可能是我的球和得分周围的物理机构都接触到了。我很茫然,任何帮助都会受到赞赏!
答案 0 :(得分:1)
我已经弄清楚了。我之后在代码中为我的orangeBall重新创建了一个physicsBody。形成另一个物理体围绕它,使它与我的其他节点发生碰撞!