EXC_BREAKPOINT - 使用swift spritekit进行冲突检测

时间:2023-01-23 07:40:07

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)

碰撞目前有效,我可以玩我的游戏一段时间但是大约50个左右的敌舰被摧毁后我得到错误EXC_BREAKPOINT(代码= 1,子代码= 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 个解决方案

#1


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

您可能想要在projectileDidCollideWithEnemy中移除射弹和敌人之前尝试检查父级是否为非零。

#1


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

您可能想要在projectileDidCollideWithEnemy中移除射弹和敌人之前尝试检查父级是否为非零。