SwiftProgger
SwiftProgger

Reputation: 11

Swift SpriteKit run SKAction in SKAction.runBlock()

Can you tell me why this code doesn't work? Isn't it possible to run an Action in a SKAction.runBlock()-function?

let testaction = SKAction.repeatActionForever(SKAction.runBlock({self.myFunction()}))
runAction(testaction)

Here is my testaction:

func myFunction() {
    runAction(SKAction.runBlock({
      let i = 0
      print(i)
    }))
}

I need this for a game. I want to distinguish some cases in myFunction to run different Actions in different cases. What have i done wrong?

Edit: When i change myFunction() to this, i get printed the 1 forever, but not the 0 from the inside runBlock.

func myFunction() {
    let j = 1
    print(j)
    runAction(SKAction.runBlock({
      let i = 0
      print(i)
    }))
}

Upvotes: 0

Views: 2060

Answers (1)

crashoverride777
crashoverride777

Reputation: 10664

Like people above have said you just need to add a slight delay to it and it should work

  let testaction1 = SKAction.runBlock(self.myFunction)
  let testaction2 = SKAction.waitForDuration(0.1)
  let sequence = SKAction.repeatActionForever(SKAction.sequence([testaction1, testaction2]))

  runAction(sequence)

Upvotes: 1

Related Questions