I have a SpriteKit game where you must shoot down asteroids before they hit the base. But if you shoot and hit the asteroid and/or the base when the asteroid is hitting the base, the program crashes.
Here's my physics contact code.
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
}
}
}