1

So I was just messin around in xcode and I ran into a problem. I created a system where every time the user touches any where on the screen it creates a SKLabel with a random color where the user touches. How could I have the SKLabels that are created disappear or be removed from the scene after say like 5 seconds? Thanks

import SpriteKit

class GameScene: SKScene {

func createNateLabel(touchLocation: CGPoint){

    let nate = SKLabelNode(fontNamed: "Chalduster")

    let randomNumber = Int(arc4random_uniform(UInt32(8)))

    if randomNumber == 0 {

        nate.fontColor = UIColor.cyanColor()

    }

    if randomNumber == 1{

        nate.fontColor = UIColor.redColor()

    }
    if randomNumber == 2{

        nate.fontColor = UIColor.blueColor()

    }
    if randomNumber == 3{

        nate.fontColor = UIColor.purpleColor()

    }
    if randomNumber == 4{

        nate.fontColor = UIColor.yellowColor()

    }
    if randomNumber == 5{

        nate.fontColor = UIColor.greenColor()

    }
    if randomNumber == 6{

        nate.fontColor = UIColor.orangeColor()

    }
    if randomNumber == 7{

        nate.fontColor = UIColor.darkGrayColor()

    }
    if randomNumber == 8{

        nate.fontColor = UIColor.yellowColor()

    }

    if nate == true{

        let wait = SKAction.waitForDuration(3)

        nate.runAction(wait)


        nate.removeAllChildren()
    }


    nate.text = "Nate"
    nate.fontSize = 35
    nate.position = touchLocation
    addChild(nate)

}

override func didMoveToView(view: SKView) {

}

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

    let touch = touches.first! as UITouch
    let touchLocation = touch.locationInNode(self)

        createNateLabel(touchLocation)

    }
}

func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
}
Charles
  • 31
  • 3

1 Answers1

1

Instead of your current waitForDuration action, use a sequence SKAction that includes a waitForDuration action followed by a removeFromParent action.

Note that your current removeAllChildren call does nothing as your label node has no children to remove.

(Edit: corrected group to sequence)


(Edit by @vacawama): I didn't feel a need for a second answer, so I'm adding this to @AliBeadle's fine answer. Here is a functioning createNateLabel which removes the label after 5 seconds using an SKAction.sequence. I also put the colors in an array to make selecting a random one cleaner:

func createNateLabel(touchLocation: CGPoint){

    let nate = SKLabelNode(fontNamed: "Chalkduster")

    let colors: [UIColor] = [.cyanColor(), .redColor(), .blueColor(), .purpleColor(), .yellowColor(), .greenColor(), .orangeColor(), .darkGrayColor(), .yellowColor()]

    nate.text = "Nate"
    nate.fontSize = 35
    nate.fontColor = colors[Int(arc4random_uniform(UInt32(colors.count)))]
    nate.position = touchLocation
    addChild(nate)

    let wait = SKAction.waitForDuration(5)
    let remove = SKAction.removeFromParent()
    let sequence = SKAction.sequence([wait, remove])

    nate.runAction(sequence)
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
Ali Beadle
  • 4,486
  • 3
  • 30
  • 55