0

I have an array with 30 SKSpriteNodes. I'd like to add each node to the scene one at a time, with a small time delay. This is what I have so far, but it adds all the sprites instantaneously.

   for var i = 0; i < 31; i++ {

        var boardPiece = SKShapeNode()
        boardPiece = addBigRedCircle()
        redPiecesArray.addObject(boardPiece)
        self.addChild(redPiecesArray.objectAtIndex(i) as SKNode)

        }

How could I add these objects from the array 1 at a time and so efficiently? (If you know how to do this with Obj-C, that would help to as I can probably figure out how to say the same thing with Swift)

Alan Scarpa
  • 2,734
  • 3
  • 21
  • 42

1 Answers1

0

How about using GCD? Is that a thing in Swift?

for var i = 0; i < 31; i++ {
  dispatch_after((Int64)(i * delayInSeconds * NSEC_PER_SEC), dispatch_get_main_queue(), {     
    var boardPiece = SKShapeNode()
    boardPiece = addBigRedCircle()
    redPiecesArray.addObject(boardPiece)
    self.addChild(redPiecesArray.objectAtIndex(i) as SKNode)
  })
}

(Structure of dispatch_after modified based on https://stackoverflow.com/a/24034838/2708650 )

Community
  • 1
  • 1
Ian MacDonald
  • 11,433
  • 1
  • 21
  • 41
  • I'm still really new to Swift and Obj-C. I looked it up and GCD with Swift is definitely do-able. I just don't understand GCD unfortunately.http://fruitstandsoftware.com/blog/2014/06/09/gcd-in-swift-it-just-works/ – Alan Scarpa Oct 30 '14 at 16:08
  • Did you try just running the code I wrote there? It looks like those functions should still exist with that API in Swift. I don't develop in Swift, so I don't have a sandbox I could test it in. – Ian MacDonald Oct 30 '14 at 16:16
  • Yea, I tried pasting your code. But Swift doesn't seem to have int64_t but it does have Int64. So when I tried Int64 I get the error "Cannot invoke '*' with an argument list of type '(dispatch_time_t, dispatch_queue_t!, () -> () -> $T15)' – Alan Scarpa Oct 30 '14 at 16:26