2016-06-24 4 views
0

私はボタンを押すと、プレーヤー(circuloPrincipal)がボタンによって決定された色に変化します。 ゲームを実行すると、以下のエラーでクラッシュすることがあります。私は解釈する方法を知っているのでエラーを見つけることはできませんアプリの実行中にキャッチされなかった例外のためアプリが終了する

キャッチされていない例外 'NSInvalidArgumentException'のためにアプリケーションを終了する理由: '既に親:name:' button4 'テクスチャを持つSKNodeを追加しようとしました。 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

私はあなたのコードからできることを実行しています。うまくいきます。しかし、コードが見つからないという大きな塊があります(私は影響を受ける行をコメントしました)。あなたがそこで起こっていることを見ることができるように敵の機能を投稿できますか? –

+0

確かに、そしてRon時々起こる –

答えて

0

ノードを追加しようとしましたが、すでに親があります。そのエラーメッセージの中にその権利があります。

あなたはそれがすでにこの

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

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

これはファインフェイルセーフですが、なぜオブジェクトを2回追加しようとしているのかという問題は解決していません。 –

+0

ええ、彼はそれをする必要はありませんが、彼はそれに対処したくない場合、彼はちょうど落ちて安全 – hamobi

+0

を使用することができますが、どこにそのコードを追加するのですか?ボタン4の前に? –

関連する問題