Panno123
Panno123

Reputation: 23

Sprite Kit Collision error - Swift

I am new to IOS dev and am currently having some issues with sprite kit collisions in the didBeginContact method.

How do I break out of, or stop didBeginContact from running if one of the colliding physics bodies are removed. eg: 1 bullet collides with 2 overlapping enemies. Because the bullet hits enemy one and is destroyed, the collision check running on the second enemy throws an exception because the bullet no longer exists.

I have tried checking for nil and NSNULL values with no luck. The error code that I receive is "Thread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOIP,subcode=0x0)" and occurs when trying to check the category bit mask (Because the torpedo no longer exists).

CODE:

    var bodyA:SKPhysicsBody = contact.bodyA
    var bodyB:SKPhysicsBody = contact.bodyB


    if(contact.bodyA.categoryBitMask == alienCategory && contact.bodyB.categoryBitMask == alienCategory){
        return
    }


    if((bodyA.categoryBitMask == alienCategory) && (bodyB.categoryBitMask == photonTorpedoCategory)){
        torpedoDidCollideWithAlien(bodyA.node as SKSpriteNode)

    }

    var currTorpedo:SKSpriteNode = contact.bodyB.node as SKSpriteNode
    var currAlien:SKSpriteNode = contact.bodyA.node as SKSpriteNode

    currTorpedo.removeFromParent()
    currAlien.removeFromParent()


}

func torpedoDidCollideWithAlien(alien:SKSpriteNode){

    aliensDestroyed++
    label.text = ("Current Score: " + String(aliensDestroyed))


}

Upvotes: 2

Views: 1373

Answers (1)

Jon
Jon

Reputation: 3238

Perhaps you could delay removing the torpedo from the scene until the next scene 'update'. You could flag the torpedo as disabled/inactive so it doesn't impact your 2nd overlapping enemy. Something like this:

class YourScene: SkScene {

    // Array of disabled torpedos
    var disabledTorpedos = SKSpriteNode()[]

    func didBeginContact(contact: SKPhysicsContact) {
        var bodyA:SKPhysicsBody = contact.bodyA
        var bodyB:SKPhysicsBody = contact.bodyB


        if(contact.bodyA.categoryBitMask == alienCategory && contact.bodyB.categoryBitMask == alienCategory){
            return
        }


        if((bodyA.categoryBitMask == alienCategory) && (bodyB.categoryBitMask == photonTorpedoCategory)){
            torpedoDidCollideWithAlien(bodyA.node as SKSpriteNode)

        }

        var currTorpedo:SKSpriteNode = contact.bodyB.node as SKSpriteNode
        var currAlien:SKSpriteNode = contact.bodyA.node as SKSpriteNode

        // Created a new photonTorpedoDisabledCategory so the torpedo cannot hit a 2nd alien
        currTorpedo.physicsBody.categoryBitMask = photonTorpedoDisabledCategory
        disabledTorpedoes.append(currTorpedo)
        // Hide torpedo from scene
        currTorpedo.hidden = true

        currAlien.removeFromParent()
    }

    override func update(currentTime: NSTimeInterval) {

        // loop through disabledTorpedos array and removeFromParent

    }

 }

Upvotes: 1

Related Questions