Reputation:
I am trying to make a ball change location 10 times:
for _ in 10 {
let ballRandomX = Int(arc4random_uniform(896)) + 64
let ballRandomY = Int(arc4random_uniform(640)) + 64
let moveAction = SKAction.moveTo(CGPoint(x: ball1RandomX, y: ball1RandomY), duration: 1.0)
self.ball.runAction(moveAction)
}
But the ball only moves once. I found that all the looping is made before the animation finishes. That means when the first move is made, the second loop comes and changes the ball destination. The computer does this so fast, it looks as it is making only one move. The most obvious solution is to stop the looping until the animation finishes. I've tried pause, but it doesn't work. Any ideas?
Upvotes: 1
Views: 71
Reputation: 499
What you want to use is a sequence action.
var allActions = [SKAction]()
for _ in 0..<10 {
let ballRandomX = Int(arc4random_uniform(896)) + 64
let ballRandomY = Int(arc4random_uniform(640)) + 64
let moveAction = SKAction.moveTo(CGPoint(x: ball1RandomX, y: ball1RandomY), duration: 1.0)
allActions.append(moveAction)
}
let sequenceAction = SKAction.sequence(allActions)
self.ball.runAction(sequenceAction)
That will run all the actions one at a time in sequence.
Upvotes: 4