1
votes

I am trying to create a specific type of animation that requires a sound and animation to occur for exactly 1 second per texture cycle.

Each time someone clicks a button, it appends a new texture to an array of SKTextures[], then I have an SKSpriteNode animate through each of the textures once. Only problem is, I can't seem to figure out a way to play a sound file for each texture as it animates. I would also like to create an animation for each texture the SKSpriteNode changes to as it goes through the textures.

Here is an example of the flow:

Button Clicked

Append SKTexture array

SKSpriteNode.animateUsingTextures(SKTextureArray, timePerFrame: 1)

While each individual texture displays, animate it, play sound.

Here is what I have that is not working.

(Code is just an abstraction of what I have)

var textures: [SKTexture] = []
let sprite: SKSpriteNode()
let scaleUp = SKAction.scaleTo(300, duration: 0.2)
let scaleDown = SKAction.scaleTo(200, duration: 0.2)
let popAnimation = SKAction.sequence([scaleUp, scaleDown])

func buttonClicked() {
    textures.append("Specific Texture")
    sprite.animation() // Calls Animation Function
}

func animation() {
    let sound = SKAction.playSoundFileNamed("DisplayGesture.mp3", waitForCompletion: true)
    let animation = SKAction.animateWithTextures(textures, timePerFrame: 1)
    let completeAnimation = SKAction.group([sound, animation, popAnimation])

    sprite.runAction(completeAnimation)
}
1
What do you mean by "Specific Texture"?Pranav Wadhwa
I'm just using abstraction to refer to a random texture that I would append. I wouldn't focus too much on that. The main thing to consider is the animation through an array of 3 or more textures.Electric
Ok. Just making sure. ThanksPranav Wadhwa
The only thing that I have managed to think up was, with the animateWithTextures method, it contains an argument timePerFrame, which is an NSTimeInterval. If there was some way to maybe override the animateWithTextures method and take advantage of the timePerFrame to just incorporate a sound and popAnimation per texture, that would most likely work.Electric
Also, you could try to to add a quick pause into the group (completeAnimation). E.g. (sound, animation, pause). Let me do some research to see if we can do either of these 2 ideas.Pranav Wadhwa

1 Answers

1
votes

I think what is happening is that you play the sound once, then animate all of the textures. What you could try doing:

fund animation() {
     for myTexture in textures {
          let sound = SKAction.playSoundFileNamed("DisplayGesture.mp3", waitForCompletion: true)
          sprite.texture = myTexture
     }
}