Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addChild after 2 seconds

I'm trying to addChild after 2 seconds the user touched the screen (it is inside touchesBegan), but it doesn't work. Am I doing anything wrong?

//show myLabel after 2 seconds
self.myLabel.position = CGPoint(x: self.frame.width / 1.1, y: self.frame.height / 2)
self.myLabel.text = "0"
self.myLabel.zPosition = 4

self.myLabel.runAction(SKAction.sequence([SKAction.waitForDuration(2.0), SKAction.runBlock({
    self.addChild(self.myLabel)
)]))
like image 386
Luiz Avatar asked Apr 03 '16 00:04

Luiz


1 Answers

The problem is that the actions won't run on myLabel unless it is in the scene, so change the last part into :

self.runAction(SKAction.sequence([SKAction.waitForDuration(2.0), SKAction.runBlock({
    self.addChild(self.myLabel)
})]))

Or better:

self.runAction(SKAction.waitForDuration(2)) {
    self.addChild(self.myLabel)
}

Note: I assume here that self is a scene or some other node that is already added to the scene.

like image 90
smallfinity Avatar answered Oct 05 '22 00:10

smallfinity