【发布时间】:2015-01-18 22:15:36
【问题描述】:
当 2 个节点发生碰撞时,我不断收到错误消息,这是我用于碰撞检测的代码:
func didBeginContact(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 & coin2Category) != 0 && (secondBody.categoryBitMask & playerCategory) != 0){
var bodyAAC = contact.bodyA.node as SKSpriteNode //error pointing at this line: fatal error: unexpectedly found nil while unwrapping an optional value
if CGRectIntersectsRect(kikker.frame, bodyAAC.frame) {
coinslos = coinslos + 10
coinlabel.text = "\(coinslos)"
bodyAAC.removeFromParent()
coinSound()
}
}
}
我做错了什么?
【问题讨论】:
-
可能contact.bodyA.node 不是
SKSpriteNode。您正在检查碰撞的物体类型是什么?
标签: swift sprite-kit collision-detection skphysicsbody