我在swift中有以下两个函数用于SKEmitterNode和SKSpriteNode之间的碰撞检测但是每当我的粒子发射器和精灵碰撞时我都会得到一个dynamicCastFailed异常:
func projectileDidCollideWithMonster(projectile:SKEmitterNode, monster:SKSpriteNode) {
println("Hit")
projectile.removeFromParent()
monster.removeFromParent()
}
func didBeginContact(contact: SKPhysicsContact) {
// 1
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
}
// 2
if ((firstBody.categoryBitMask & PhysicsCategory.Monster != 0) &&
(secondBody.categoryBitMask & PhysicsCategory.Projectile != 0)) {
projectileDidCollideWithMonster(firstBody.node as SKEmitterNode, monster: secondBody.node as SKSpriteNode)
}
}
是否可以使SKEmitterNodes
与SKSpriteNodes
发生冲突,或者这不起作用?
答案 0 :(得分:1)
你的身体已经切换了。我相信它应该是这样的:
projectileDidCollideWithMonster(secondBody.node as SKEmitterNode, monster: firstBody.node as SKSpriteNode)