2016-06-24 3 views
0

버튼을 누르면 플레이어 (circuloPrincipal)가 버튼으로 결정된 색상으로 변경되는 게임을 만들고 있습니다. 게임을 실행할 때 아래의 오류로 인해 때때로 충돌이 발생합니다. 내가 이해하는 방법을 알기 때문에 오류를 발견하지 못한다.앱 실행 중 캐치되지 않는 예외로 인해 앱 종료

캐치되지 않은 예외 'NSInvalidArgumentException'으로 인해 앱 종료 중, 이유 : '이미 부모가있는 SKNode를 추가하려고 시도 함 :'button4 'texture : [' circuloBoton '(100 × 100)]의 위치 : {407, 39} 규모 : {1.00, 1.00}의 크기 : {60, 60} 앵커 {0.5, 0.5} 회전 : 0.00'가있어

circuloPrincipal.size = CGSize(width: 225, height: 225) 
    circuloPrincipal.position = CGPoint(x: frame.width/2, y: frame.height/2) 
    circuloPrincipal.color = colorAzul 
    circuloPrincipal.colorBlendFactor = 1.0 
    circuloPrincipal.physicsBody = SKPhysicsBody(circleOfRadius: circuloPrincipal.size.height/2) 
    circuloPrincipal.physicsBody?.contactTestBitMask = physicsCategory.circuloPrincipal 
    circuloPrincipal.physicsBody?.collisionBitMask = physicsCategory.enemigo 
    circuloPrincipal.physicsBody?.contactTestBitMask = physicsCategory.enemigo 
    circuloPrincipal.physicsBody?.affectedByGravity = false 
    circuloPrincipal.physicsBody?.dynamic = false 
    circuloPrincipal.name = "circuloPrincipal" 
    circuloPrincipal.zPosition = 5.0 
    circuloBlanco.size = CGSize(width: 60, height: 60) 
    circuloBlanco.position = CGPoint(x: frame.width/2 - 105 , y: frame.height/2 - 345) 
    circuloBlanco.color = colorBlanco 
    circuloBlanco.colorBlendFactor = 1.0 
    circuloBlanco.zPosition = 4.0 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    /* Called when a touch begins */ 

    if gameStarted == false { 

     circuloVerde.removeFromParent() 
     circuloMorado.removeFromParent() 
     circuloRojo.removeFromParent() 
     circuloBlanco.removeFromParent() 


     circuloPrincipal.color = colorAzul 

     enemigoTimer = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: Selector("enemigos"), userInfo: nil, repeats: true) 

     gameStarted = true 
     circuloPrincipal.runAction(SKAction.scaleTo(0.44, duration: 0.4)) 
     score = 0 
     scoreLabel.text = "\(score)" 
     hits = 0 
     highscoreLabel.runAction(SKAction.fadeOutWithDuration(0.5))  
    } 

    circuloVerde.name = "button" 

    let touch = touches.first 
    let positionInScene = touch!.locationInNode(self) 
    let touchedNode = self.nodeAtPoint(positionInScene) 

    if let name = touchedNode.name { 
     if name == "button" {  
      circuloPrincipal.color = colorVerde 
     } 
    } 

    circuloAmarillo.name = "button2" 

    let touch2 = touches.first 
    let positionInScene2 = touch2!.locationInNode(self) 
    let touchedNode2 = self.nodeAtPoint(positionInScene2) 

    if let name = touchedNode2.name { 
     if name == "button2" { 
      circuloPrincipal.color = colorAmarillo  
     } 
    } 

    circuloAzul.name = "button3" 

    let touch3 = touches.first 
    let positionInScene3 = touch3!.locationInNode(self) 
    let touchedNode3 = self.nodeAtPoint(positionInScene3) 

    if let name = touchedNode3.name { 
     if name == "button3" { 
      //do my stuff 
      circuloPrincipal.color = colorAzul 
     } 
    } 

    circuloBlanco.name = "button4" 

    let touch4 = touches.first 
    let positionInScene4 = touch4!.locationInNode(self) 
    let touchedNode4 = self.nodeAtPoint(positionInScene4) 

    if let name = touchedNode4.name { 
     if name == "button4" { 
      circuloPrincipal.color = colorBlanco 
     } 
    } 

    circuloRojo.name = "button5" 

    let touch5 = touches.first 
    let positionInScene5 = touch5!.locationInNode(self) 
    let touchedNode5 = self.nodeAtPoint(positionInScene5) 

    if let name = touchedNode5.name { 
     if name == "button5" { 
      circuloPrincipal.color = colorRojo 
     } 
    } 

    circuloMorado.name = "button6" 

    let touch6 = touches.first 
    let positionInScene6 = touch6!.locationInNode(self) 

    let touchedNode6 = self.nodeAtPoint(positionInScene6) 

    if let name = touchedNode6.name { 
     if name == "button6" { 
      circuloPrincipal.color = colorMorado 
     } 
    } 
} 
+0

악의 같은 부모가 있는지 확인할 수 있습니다,하지만 당신의 코드는 엉망이었다. 사람들에게 그것을 읽으라고 요구하기 전에 여분의 공백을 모두 정리하고 포맷해야합니다. –

+0

저는 여러분의 코드에서 할 수있는 것을 실행하고 있습니다. 하지만 엄청난 양의 코드가 누락되었습니다 (방금 영향을받은 행을 주석 처리했습니다). 내가 거기에서 무슨 일이 일어나고 있는지 볼 수 있도록 enemigos 함수를 게시 할 수 있습니까? –

+0

확실하고 론 때로는 발생 –

답변

0

노드를 추가하려하지만 이미 노드가 있습니다. 바로 그 오류 메시지에.

당신은 이미이

let childNode = SKNode() 
let parentNode = SKNode() 

if childNode.parent == nil { 
    parentNode.addChild(childNode) 
} 
+0

이것은 괜찮아요,하지만 그는 두 번 객체를 추가하려고하는 문제를 해결하지 못합니다. –

+0

그래, 그는 그럴 필요가 없지만 만약 그걸 해결하고 싶지 않다면 낙하산을 사용할 수 있습니다 – hamobi

+0

하지만 그 코드를 어디에 추가합니까 ?? 버튼 4 이전에? –

관련 문제