Casey
Casey

Reputation: 11

How can I create a loop that runs two functions, never overlapping?

I'm new to programming and have been taking online courses in swift and spritekit trying to create my first working game from scratch.

Currently I'm having an issue trying to create a sequence of functions that run independently, wait so that only one is running at a time, and loops indefinitely.

The first function:

func shootTwentyArrows() {

    var oneArrow = SKAction.runBlock{

        self.shootArrow()

    }

    var waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
    var fireAnArrow = SKAction.sequence([oneArrow, waitBetweenArrows])
    self.runAction(SKAction.repeatAction(fireAnArrow, count: 20))
}

And the second function:

func shootSpiral1() {

    var leftArrow = SKAction.runBlock{

        self.arrowFromLeft()

    }

    var rightArrow = SKAction.runBlock{

        self.arrowFromRight()

    }

    var waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
    var sequence = SKAction.sequence([leftArrow, waitBetweenArrows, rightArrow, waitBetweenArrows])

    var spiral1 = SKAction.repeatAction(sequence, count: 5)

    self.runAction(spiral1)

to clarify, I'm trying to run something like: shootTwentyArrows() when that's done, shootSpiral1(), when that's done repeat.

Thanks in advance for any responses.

Upvotes: 0

Views: 437

Answers (2)

Norm Doow
Norm Doow

Reputation: 73

The best way to do this is have an SKAction sequence that calls both of your functions. You would call this SKAction from wherever you want to initialize the arrow shooting actions like viewDidLoad for example. This would be the code to call the actions...

var actionShootingArrows = SKAction.sequence([shootSpiral1(), shootTwentyArrows()])
self.runAction(SKAction.repeatActionForever(actionShootingArrows))

Hope this helps!

Upvotes: 0

s1ddok
s1ddok

Reputation: 4654

I guess the most correct way to do that would be to refactor code a little bit:

func shootTwentyArrows() -> SKAction {

let oneArrow = SKAction.runBlock{

    self.shootArrow()

}

let waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
let fireAnArrow = SKAction.sequence([oneArrow, waitBetweenArrows])
return SKAction.repeatAction(fireAnArrow, count: 20)
}

func shootSpiral1() -> SKAction {

let leftArrow = SKAction.runBlock{

    self.arrowFromLeft()

}

let rightArrow = SKAction.runBlock{

    self.arrowFromRight()

}

let waitBetweenArrows = SKAction.waitForDuration(arrowSpeed)
let sequence = SKAction.sequence([leftArrow, waitBetweenArrows, rightArrow, waitBetweenArrows])

let spiral1 = SKAction.repeatAction(sequence, count: 5)
return spiral1
}

Then somewhere in the code you can just do something like that :

let spiralAction = shootSpiral1()
let oneArrowAction = shootTwentyArrows()
let sequence = SKAction.sequence([spiralAction, oneArrowAction])
let infiniteSequence = SKAction.repeatActionForever(sequence)
self.runAction(infiniteSequence)

I left function names the same on purpose, so you get the idea.

P.S. It is a common practice to declare a variable as let, declare it as var only when you have to modify it later.

Upvotes: 1

Related Questions