2016-07-14 7 views
0

이 코드를 실행하면 첫 번째 CollisionWithplayer 라인이 잘못된 명령어 오류를 발생시킵니다. 이 오류는 매번 나타나지 않으며, 단지 유사한 조건이 없기 때문에 발생하는 원인을 식별합니다.Swift : Physics Body Collision 잘못된 명령어 오류

func didBeginContact(contact: SKPhysicsContact) { 
    let firstBody : SKPhysicsBody = contact.bodyA 
    let secondBody : SKPhysicsBody = contact.bodyB 


    if ((firstBody.categoryBitMask == PhysicsCategory.Goblin) && (secondBody.categoryBitMask == PhysicsCategory.Bullet) || 
     (firstBody.categoryBitMask == PhysicsCategory.Bullet) && (secondBody.categoryBitMask == PhysicsCategory.Goblin)) 
    { 

     CollisionWithBullet(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode) 
    } 
     else if ((firstBody.categoryBitMask == PhysicsCategory.Goblin) && (secondBody.categoryBitMask == PhysicsCategory.player) || 
     (firstBody.categoryBitMask == PhysicsCategory.player) && (secondBody.categoryBitMask == PhysicsCategory.Goblin)){ 

     CollisionWithplayer(firstBody.node as! SKSpriteNode, player: secondBody.node as! SKSpriteNode) 
      } 

func CollisionWithBullet(Goblin: SKSpriteNode, Bullet:SKSpriteNode){ 
    Goblin.removeFromParent() 
    Bullet.removeFromParent() 
    score += 1 
    ScoreLbl.text = "\(score)" 
    var explosion = SKEmitterNode(fileNamed: "Goblin Death Animation.sks") 
    explosion!.particlePosition = Goblin.position 
    self.addChild(explosion!) 
    var fire = SKEmitterNode(fileNamed: "Goblin Death Animation 2.sks") 
    fire!.particlePosition = Goblin.position 
    self.addChild(fire!) 


} 
func CollisionWithplayer(Goblin: SKSpriteNode, player: SKSpriteNode){ 
    let ScoreDefault = NSUserDefaults.standardUserDefaults() 
    ScoreDefault.setValue(score, forKey: "Score") 
    ScoreDefault.synchronize() 


    if (score > Highscore){ 

     let HighscoreDefault = NSUserDefaults.standardUserDefaults() 
     HighscoreDefault.setValue(score, forKey: "Highscore") 

    } 

    Goblin.removeFromParent() 
    player.removeFromParent() 
    self.view?.presentScene(EndScene()) 
    ScoreLbl.removeFromSuperview() 
} 

답변

1

나는 코드가 1 개 충돌이

나는 것 (충돌이 동일한 노드의 2 개 지점에서 발생)이 didBeginContact 방법은 한 번 이상 화재의 원인이되는 경우를 취급하지 않기 때문에 오류가 발생하는 가정 이런 경우를 피하기 위해 (옵션을 사용하여) 코드를 다시 작성하십시오. 또한 각 충돌마다 2 if 문을 작성할 필요가 없도록 약간 재 작성했습니다.

func didBeginContact(contact: SKPhysicsContact) { 
    let firstBody: SKPhysicsBody 
    let 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.Goblin) && (secondBody.categoryBitMask == PhysicsCategory.Bullet) { 
      collisionWithBullet(firstBody.node as? SKSpriteNode, bullet: secondBody.node as? SKSpriteNode) 
    } 

    if (firstBody.categoryBitMask == PhysicsCategory.Goblin) && (secondBody.categoryBitMask == PhysicsCategory.player) { 
      collisionWithPlayer(firstBody.node as? SKSpriteNode, player: secondBody.node as? SKSpriteNode) 
    } 
} 

func collisionWithBullet(goblin: SKSpriteNode?, bullet:SKSpriteNode?){ 
    guard let goblin = goblin, bullet = bullet else { return } 

    goblin.removeFromParent() 
    bullet.removeFromParent() 
    score += 1 
    scoreLbl.text = "\(score)" 
    if let explosion = SKEmitterNode(fileNamed: "Goblin Death Animation.sks") { 
      explosion.particlePosition = goblin.position 
      self.addChild(explosion) 
     } 
     if let fire = SKEmitterNode(fileNamed: "Goblin Death Animation 2.sks") { 
      fire.particlePosition = goblin.position 
      self.addChild(fire) 
     } 
    } 

    func collisionWithPlayer(goblin: SKSpriteNode?, player: SKSpriteNode?){ 
    guard let goblin = goblin, player = player else { return } 

    let scoreDefault = NSUserDefaults.standardUserDefaults() 
    scoreDefault.setValue(score, forKey: "Score") 
     // synchronised not needed anymore 


    if (score > highscore){ 
     let highscoreDefault = NSUserDefaults.standardUserDefaults() 
     highscoreDefault.setValue(score, forKey: "Highscore") 
    } 

    goblin.removeFromParent() 
    player.removeFromParent() 
    self.view?.presentScene(EndScene()) 
    scoreLbl.removeFromSuperview() 
} 

신속한 안내선을 따라 가면, 귀하의 방법 및 속성이 시작됩니다. 작은 글자는 대문자로 시작하지 않을 것입니다.

희망이 있습니다.

+0

도움 주셔서 감사합니다. 답장을 늦게 보내서 미안해. 이것은 내 문제를 해결했다. – Brian

+0

당신은 환영합니다. 내 대답을 표시하고 투표를하는 것이 너무 친절하니? 행복한 코딩. – crashoverride777

+0

나는 투표 결과를 공개했지만 공개적으로 표시되기 위해서는 더 많은 평판이 필요하다고 말했습니다. 고맙습니다. – Brian

관련 문제