-1

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
  • do{...}while(true) is endless loop until you somehow change the value of y. I guess you don't want that. From the code you've posted it looks like you are trying to spawn nodes and move them? Do you need them periodically spawned? Read part about shurikens in this tutorial : http://www.raywenderlich.com/80917/sprite-kit-inverse-kinematics-swift That would be one way to spawn nodes periodically. You can still use switch to choose what node to spawn, but I think that do/while endless loop is pretty bad choice for any situation. – Whirlwind Apr 02 '15 at 23:08
  • i want to spawn infinite nodes and move them (thats why I put the endless loop), i need to spawn them each 2 seconds.. – Moises Rodan Apr 03 '15 at 00:59
  • instead of the while loop is better a func? – Moises Rodan Apr 03 '15 at 01:00

1 Answers1

1

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)
}
rakeshbs
  • 24,392
  • 7
  • 73
  • 63