简体   繁体   中英

How can I add objects to a scene from an array one at a time?

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)

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM