user5713130 user5713130 - 5 years ago 136
Swift Question

Issues with SKAction

The top function works. I have a 5 x 5 board and while an array contains 100, I need to keep generating blocks to place on the board. The check smallest keeps the smallest number on the board stored as the smallest only making that block clickable. I am wanting to spawn the blocks over time, and the second function is how I tried but just crashes the app.

func generateAndCheckBlocks() {

while smallestArr.contains(100){
if placeBlocks() == true {
checkSmallest()
}
}
}

func generateAndCheckBlocks() {

let wait = SKAction.waitForDuration(1)
let run = SKAction.runBlock {
if self.placeBlocks() == true {
self.checkSmallest()
}
}

while smallestArr.contains(100){
runAction(SKAction.sequence([run, wait]))
}
}

Answer Source

What a weird approach you have going on, you may have bigger issues then you think, but I take it this is a learning experience, so...

Instead of while loops, when your game needs to start creating blocks, just do:

//Only call this once, not in a loop
func generateBlocks() {

    let wait = SKAction.waitForDuration(1)
    let run = SKAction.runBlock {
        if self.placeBlocks() == true {
            self.checkSmallest()
        }
    }
    self.runAction(SKAction.repeatActionForever(SKAction.sequence([run, wait])), withKey:"blocks");


}

Then, you can do this in your update loop:

if(!smallestArr.contains(100))
{
    self.removeActionForKey("blocks")
}    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download