某些情况下不会出现SpriteNode

时间:2016-06-25 21:17:29

标签: swift sprite-kit skspritenode

我正在制作一个游戏,在某些点上会出现一个新按钮和一个标签。问题是有些情况下都不会出现。

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

1 个答案:

答案 0 :(得分:0)

我认为你已经失去了这条线:

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

这是必要的,因为你removeFromParent这个元素,所以下次你调用enemigos函数时,我看到你改变了circuloBlanco参数。