Jason Krowl
Jason Krowl

Reputation: 114

How to keep track of animation in Sprite Kit

I need to keep track of animation with texture. I am animating power bar and when user clicks the screen it should stop and save the power. I can not figure out how to save power. So far I have this: on first touch power bar animates, but on the second touch it only stops but does not save power.

This is how I create animation:

textureAtlas = SKTextureAtlas(named:"images")
        for i in 1...textureAtlas.textureNames.count{
            let name = "\(i).png"
            textureArray.append(SKTexture(imageNamed: name))
        }
 let animateForward = SKAction.animate(with: textureArray, timePerFrame: 0.1)
        let animateBackward = SKAction.reversed(animateForward)
        let sequence = SKAction.sequence([animateForward,animateBackward()])

This is how I detect touches:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let firstTouchStorage = touches.first{ // after first touch
        let animateForward = SKAction.animate(with: textureArray, timePerFrame: 0.1)
        let animateBackward = SKAction.reversed(animateForward)
        let sequence = SKAction.sequence([animateForward,animateBackward()])
        arrow.removeAllActions()
        arrow.run(SKAction.repeatForever(sequence))
        firstTouch = firstTouchStorage
    }
    for touch in touches{
        if touch == firstTouch{
            touchesArray.append(touch)

            let angle = arrow.zRotation
            if touchesArray.count == 2{
                arrow.removeAllActions()
                arrow.removeFromParent()
            }
        }
    }

I am trying to solve this problem for too long, but I can not figure it out. I hope you will help me.

Upvotes: 2

Views: 137

Answers (1)

Jason Krowl
Jason Krowl

Reputation: 114

As @KnightOfDragon suggested : animateWithTextures has a restore: parameter, if you set that to false, when you stop animating the last texture should stay on the sprite. If you go ahead and read the textures description, then you will know what texture it stopped on, and could plan accordingly

Upvotes: 0

Related Questions