Moises Rodan
Moises Rodan

Reputation: 71

How to add child (sknode) in a Switch Statement in swift spritekit

I'm making a litle game, and I need to have random columns runing from right to left. I'm using a switch statement. In each case I am adding a diferent child node, but it doesn't work.

All g1, g2.. here..



var y = true
do{
var x = arc4random_uniform(5)
switch x
  {
case 1: addChild(g1); g1.runAction(moveremover)
case 2: addChild(g2); g2.runAction(moveremover)
default: SKAction.waitForDuration(2)
  }
 }
while y

Upvotes: -1

Views: 541

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

Instead of a endless do while loop, you can use SKAction.repeatForever to repeat a function again and again with a delay.

func spawnColumn () {
    // This has your column spawning code.
}

func spawnColumnEveryTwoSeconds() {

    let spawnAction = SKAction.runBlock { () -> Void in
        self.spawnColumn()
    }

    let waitTwoSeconds = SKAction.waitForDuration(2.0)
    let spawnAndWait = SKAction.sequence([spawnAction,waitTwoSeconds])
    let spawnAndWaitForever = SKAction.repeatActionForever(spawnAndWait)

    self.runAction(spawnAndWaitForever)
}

Upvotes: 1

Related Questions