sprite-kit
физика
Поиск…
Как правильно удалить узел в методе didBeginContact (несколько контактов)
// PHYSICS CONSTANTS
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let player : UInt32 = 0b1 // 1
static let bullet : UInt32 = 0b10 // 2
}
var nodesToRemove = [SKNode]()
// #-#-#-#-#-#-#-#-#-#-#-#-#-#-#
//MARK: - Physic Contact Delegate methods
// #-#-#-#-#-#-#-#-#-#-#-#-#-#-#
func didBegin(_ contact: SKPhysicsContact) {
var one: SKPhysicsBody
var two: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
one = contact.bodyA
two = contact.bodyB
} else {
one = contact.bodyB
two = contact.bodyA
}
// PLAYER AND BULLET
if one.categoryBitMask == PhysicsCategory.player && two.categoryBitMask == PhysicsCategory.bullet {
nodesToRemove.append(one.node!) // remove player
nodesToRemove.append(two.node!) // remove bullet
}
}
override func didFinishUpdate()
{
nodesToRemove.forEach(){$0.removeFromParent()}
nodesToRemove = [SKNode]()
}
}
Modified text is an extract of the original Stack Overflow Documentation
Лицензировано согласно CC BY-SA 3.0
Не связан с Stack Overflow