1

The collision currently works and I can play my game for a while but after about 50 or so enemy ships are destroyed I get the error EXC_BREAKPOINT (code=1, subcode=0x1003f42cc)

I am getting it on this line of code

projectileDidCollideWithEnemy(firstBody.node as SKSpriteNode, enemy: secondBody.node as SKSpriteNode)

Here are the bits of code that I believe are effecting it and that the breakpoint leads to.

func didBeginContact(contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }
    else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if ((firstBody.categoryBitMask & PhysicsCategory.Enemy != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.Projectile != 0)) {
            projectileDidCollideWithEnemy(firstBody.node as SKSpriteNode, enemy: secondBody.node as SKSpriteNode)
    }

And here is the func that gets called

func projectileDidCollideWithEnemy(projectile:SKSpriteNode, enemy:SKSpriteNode) {
    projectile.removeFromParent()
    enemy.removeFromParent()
    score++
    scorelabel.text = String(score)
}

1 Answer 1

0

You may want to try checking if the parent is non-nil before removing the projectile and enemy in projectileDidCollideWithEnemy.

Sign up to request clarification or add additional context in comments.

5 Comments

I fixed that but it is still happening. After more testing it seems like it only happens when a projectile is able to destroy 2 enemies at once. Any ideas?
In that case, it sounds like the projectile is being removed twice but it's not there for the second time. Try putting an exception breakpoint in so you can see more specifically which line within the method is causing the crash. Is this still only happening after 50 ships destroyed or any time you hit two enemies with the same projectile? Also are each projectile and enemy created as you need them or are you batch creating many copies of each at the start of the game?
The enemies and projectiles are only created when needed not just all at the beginning. It happens anytime they can both be destroyed not after a specific number. I had not thought about it that way but that makes perfect sense. I will set a breakpoint and try to figure out the logic. Thanks
The problem is when a projectile hits 2 ships at a time. The break point leads to this error "fatal error: unexpectedly found nil while unwrapping an Optional value" I believe this happens because it tries to destroy the same projectile twice like you said but I have no idea how to fix this. Any ideas, suggestions?
1) Where exactly does the breakpoint stop executing the code? On "projectile.removeFromParent()" ? 2) The code above doesn't look like is has any Optionals, I don't see any question marks. Are you using Swift 1.2? The syntax for "as SKSpriteNode" should now be "as?" or "as!". 3) Right before calling "projectileDidCollideWithEnemy", try setting the projectile's physicsBody to nil. Hopefully this will prevent the next contact from happening. If that doesn't work, also add the conditional: if secondBody != nil && secondBody.node.parent != nil { projectileDidCollideWithEnemy...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.