2016-06-25 11 views
1

ある時点で新しいボタンとラベルが表示されるゲームを作成しています。問題は、どちらも現れない場合があることです。SpriteNodeが表示されない場合があります

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

    /* Called when a touch begins */ 
    circuloAmarillo.name = "button2" 

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

    let touchedNode2 = self.nodeAtPoint(positionInScene2) 

    if let name = touchedNode2.name { 

     print("name : \(name)") 
     if name == "button2" { 
      //do my stuff 
      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 { 
     print("name : \(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 { 

     print("name : \(name)") 
     if name == "button4" { 
      //do my stuff 
      circuloPrincipal.color = colorBlanco 
     } 
    } 

    if gameStarted == false { 

     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)) 
    } 
} 

func enemigos() { 

    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 

    textLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium") 

    textLabel.text = "New color added" 
    textLabel.fontSize = 35 
    textLabel.position = CGPoint(x: frame.width/2, y: frame.height/2 + 147) 
    textLabel.fontColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0) 
    textLabel.colorBlendFactor = 1.0 
    textLabel.zPosition = 2.0 

    linea = SKSpriteNode(imageNamed: "linea") 

    linea.position = CGPoint(x: frame.width/2, y: frame.height/2 + 160) 
    linea.size = CGSize(width: frame.width, height: 50) 
    linea.alpha = 0.3 
    linea.zPosition = 1.0 

    let enemigo = SKSpriteNode(imageNamed: "enemigo") 
    enemigo.size = CGSize(width: 25, height: 25) 
    enemigo.zPosition = 3.0 
    enemigo.physicsBody = SKPhysicsBody(circleOfRadius: enemigo.size.height/2) 
    enemigo.physicsBody?.categoryBitMask = physicsCategory.enemigo 
    enemigo.physicsBody?.contactTestBitMask = physicsCategory.circuloPrincipal 
    enemigo.physicsBody?.collisionBitMask = physicsCategory.circuloPrincipal 
    enemigo.physicsBody?.dynamic = true 

    var colors = [colorAzul, colorAmarillo] 

    if score >= 22 { colors.append(colorBlanco) };let randomColorIndex = GKRandomDistribution(lowestValue: 0, highestValue: colors.count - 1).nextInt(); enemigo.color = colors[randomColorIndex] 

    if score == 20 { 

     let fadingAnimation = SKAction.sequence([SKAction.fadeInWithDuration(0.7), SKAction.fadeOutWithDuration(0.7)]) 

     if linea.parent == nil { 

      linea.runAction(fadingAnimation) 
      self.addChild(linea) 
     } 

     if textLabel.parent == nil { 

      textLabel.runAction(fadingAnimation) 
      self.addChild(textLabel) 
     } 

     if circuloBlanco.parent == nil { 
      self.addChild(circuloBlanco) 
     } 
    } 
}  

答えて

0

私はあなたがこの行を失っていると思う:

func enemigos() { 
    circuloBlanco = SKSpriteNode(imageNamed:"nameOfYourWhiteButton.png") 
    ... 
} 

ことが必要であるので、あなたremoveFromParentこの要素、あなたはenemigos関数を呼び出す次回ので、私はあなたがcirculoBlancoパラメータを変更ご覧ください。

関連する問題