测试节点是否附加到 physicsBody swift
testing if a node is attached to a physicsBody swift
我在检测与 SKSpriteNode 之间的接触时调用的函数中获取连接到接触体 (SKPhysicsBody) 的节点。有时,我会得到一个错误,因为它无法将节点附加到接触体,所以我测试是否有一个节点可以避免这种类型的错误。但是警告告诉我它永远不会 return false。自从我测试以来,我再也没有遇到过这样的错误,但我不确定它是否运行良好或者是否仍然会发生。你有什么想法吗?
//The actual code
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){ //test if it is the contact with I want to catch
let shootNode = contact.bodyB.node, shootNode != nil { // I test if there's a node attached to the enemyShootNode but it's supposed never to return false
let enemyNode = contact.bodyA.node, enemyNode != nil { // I test if there's a node attached to the shootNode but it's supposed never to return false
let enemySKNode = enemyNode as? SKSpriteNode
let shootSKNode = shootNode as? SKSpriteNode
// code
}
}
}
}
// The code with error
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){
let shootNode = contact.bodyB.node!
let enemyNode = contact.bodyA.node! // The error occurs here : "fatal error: unexpectedly found nil while unwrapping an Optional value"
let enemySKNode = enemyNode as? SKSpriteNode
let shootSKNode = shootNode as? SKSpriteNode
}
}
感谢您的指点,错误确实来自于 SKPhysics 属性(didBegin 函数被调用了多次),但我没有设法修复。
虽然我更正了我检查精灵是否在这里的方式,但没有更多的警告或错误,所以我认为它运行良好:
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory) || (contact.bodyA.categoryBitMask == shootCategory) && (contact.bodyB.categoryBitMask == enemyCategory){
if var shootNode = contact.bodyB.node{
if var enemyNode = contact.bodyA.node{
// If the enemyNode and the shootNode don't respectively correspond to
if contact.bodyA.categoryBitMask == shootCategory {
shootNode = contact.bodyA.node!
enemyNode = contact.bodyB.node!
}
let enemySKNode = enemyNode as? SKSpriteNode
let shootSKNode = shootNode as? SKSpriteNode
}
}
}
}
我在检测与 SKSpriteNode 之间的接触时调用的函数中获取连接到接触体 (SKPhysicsBody) 的节点。有时,我会得到一个错误,因为它无法将节点附加到接触体,所以我测试是否有一个节点可以避免这种类型的错误。但是警告告诉我它永远不会 return false。自从我测试以来,我再也没有遇到过这样的错误,但我不确定它是否运行良好或者是否仍然会发生。你有什么想法吗?
//The actual code
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){ //test if it is the contact with I want to catch
let shootNode = contact.bodyB.node, shootNode != nil { // I test if there's a node attached to the enemyShootNode but it's supposed never to return false
let enemyNode = contact.bodyA.node, enemyNode != nil { // I test if there's a node attached to the shootNode but it's supposed never to return false
let enemySKNode = enemyNode as? SKSpriteNode
let shootSKNode = shootNode as? SKSpriteNode
// code
}
}
}
}
// The code with error
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory){
let shootNode = contact.bodyB.node!
let enemyNode = contact.bodyA.node! // The error occurs here : "fatal error: unexpectedly found nil while unwrapping an Optional value"
let enemySKNode = enemyNode as? SKSpriteNode
let shootSKNode = shootNode as? SKSpriteNode
}
}
感谢您的指点,错误确实来自于 SKPhysics 属性(didBegin 函数被调用了多次),但我没有设法修复。 虽然我更正了我检查精灵是否在这里的方式,但没有更多的警告或错误,所以我认为它运行良好:
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == enemyCategory) && (contact.bodyB.categoryBitMask == shootCategory) || (contact.bodyA.categoryBitMask == shootCategory) && (contact.bodyB.categoryBitMask == enemyCategory){
if var shootNode = contact.bodyB.node{
if var enemyNode = contact.bodyA.node{
// If the enemyNode and the shootNode don't respectively correspond to
if contact.bodyA.categoryBitMask == shootCategory {
shootNode = contact.bodyA.node!
enemyNode = contact.bodyB.node!
}
let enemySKNode = enemyNode as? SKSpriteNode
let shootSKNode = shootNode as? SKSpriteNode
}
}
}
}