2

I am trying to animate a SKSpriteNode using an array of textures. What I am trying to achieve is to iterate through the texture array and change the SKSpriteNode texture through each iteration and display it's change for about 1 second. The only problem is, the loop is continuing as the animations are taking place I believe, so I can't see the changes.

I essentially want to be able to see each texture on the screen for about a second or two before it changes. Here is what I have so far.

var textures: [SKTexture] = [texture1, texture2, texture3, texture4]
var sprite = SKSpriteNode()

func animateSpriteTextures() {

    for texture in textures {
    /* Want to pause for about a second or two here, but does not. */
    sprite.texture = texture

    let scaleDown = SKAction.scaleTo(200, duration: 1)
    let scaleUp = SKAction.scaleTo(300, duration: 1)
    sprite.runAction(SKAction.sequence([scaleDown, scaleUp]))
    }
}
Electric
  • 115
  • 10

1 Answers1

2

Take a look at the animateWithTextures: action. You could do something like:

sprite.runAction(SKAction.animateWithTextures(textures, timePerFrame: 1.0))

If you need to change the size of the sprite as the animation occurs, then you'll probably want to create an action group SKAction.group() to coordinate the animation and the size changes or maybe animateWithTextures:timePerFrame:resize:restore: would work for you.

coping
  • 805
  • 1
  • 8
  • 19