Ich habe ein SpriteKit-Spiel, in dem Sie Asteroiden abschießen müssen, bevor sie die Basis treffen. Aber wenn Sie schießen und den Asteroiden und/oder die Basis treffen, wenn der Asteroid die Basis trifft, stürzt das Programm ab.SpriteKit-Spiel stürzt ab, wenn zwei SKPhysicsContacts erkannt werden
Hier ist meine Physik Kontaktcode.
public func didBegin(_ contact: SKPhysicsContact) {
//Asteroid is object 1, other item is object 2
var object1 = SKSpriteNode()
var object2 = SKSpriteNode()
//Test for asteroid/projectile contact, then remove appropriate sprites, change game values and play sounds
if contact.bodyA.contactTestBitMask == ColliderType.Asteroid.rawValue && contact.bodyB.contactTestBitMask == ColliderType.Asteroid.rawValue{
if contact.bodyA.categoryBitMask == ColliderType.Asteroid.rawValue{
}else if contact.bodyB.categoryBitMask == ColliderType.Asteroid.rawValue{
object2 = contact.bodyA.node as! SKSpriteNode
object1 = contact.bodyB.node as! SKSpriteNode
let explosionPath = URL(fileURLWithPath: Bundle.main.path(forResource: "astd", ofType: "m4a")!)
do {
audioPlayer = try AVAudioPlayer(contentsOf: explosionPath)
} catch {
print("error")
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
if object2.physicsBody?.categoryBitMask == ColliderType.Object.rawValue{
object1.removeAllActions()
object1.removeFromParent()
let hitPath = URL(fileURLWithPath: Bundle.main.path(forResource: "craftHit", ofType: "mp3")!)
do {
audioPlayer = try AVAudioPlayer(contentsOf: hitPath)
} catch {
print("error")
}
audioPlayer.prepareToPlay()
audioPlayer.play()
reduceHealthBy(num: 0.075)
}else if object2.physicsBody?.categoryBitMask == ColliderType.Projectile.rawValue{
object1.removeAllActions()
object1.removeFromParent()
object2.removeAllActions()
object2.removeFromParent()
score += 1
}
}
}
Was ist der Fehler? –
Es ist auf Swift Playgrounds, also habe ich keinen Fehler bekommen, aber es stürzt einfach ab. Ich habe herum experimentiert und bin mir ziemlich sicher, dass es die multiplen Physikkontakte sind. – QPDev