0

I've got a problem with swift and Sprite kit. My game is supposed to detect collision between enemies and the player(spaceship). `

func didBeginContact(contact: SKPhysicsContact){

    let firstBody:SKPhysicsBody = contact.bodyA
    let secondBody:SKPhysicsBody = contact.bodyB

    if (firstBody.categoryBitMask == BodyType.bulletMask) && (secondBody.categoryBitMask == BodyType.enemyMask){
        collisionWithBullet(firstBody.node as! SKSpriteNode,person: secondBody.node as! SKSpriteNode)
    }
    if (firstBody.categoryBitMask == BodyType.spaceShipMask) && (secondBody.categoryBitMask == BodyType.enemyMask){
        collisionWithSpaceShip(firstBody.node as! SKSpriteNode,person: secondBody.node as! SKSpriteNode)
    }
}

When my spaceship hit an enemy or if the bullet hit an enemy, i'll have this error :

"fatal error: unexpectedly found nil while unwrapping an Optional value"

How can I fix this ? Thanks.

Joe
  • 7,968
  • 7
  • 33
  • 55
maito
  • 3
  • 4
  • Which line of code produces this error? Check out this question, too: http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values – jperl Jun 25 '15 at 15:38
  • It happen when the program call the function. But my function that spawn enemy contain this `asteroid.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "asteroid"), size: asteroid.size)` for the physics body. This is the source of the problem right ? – maito Jun 25 '15 at 18:01

0 Answers0