swiftswift3sprite-kitsktexture

SkSpritenode normal texture not restoring even with restore on?


Ok. I am having a very odd problem with SKTextures and animating through - I have created a sprite node like this and set the normal texture to the last image in the array of sktextures I will eventually animate through. The texture (not normal texture) is set to the first image in the array.

theSprite = SKSpriteNode()
        theSprite.texture = atlas[0]
        theSprite.normalTexture = atlas[atlas.count-1]
        theSprite.size = CGSize(width: 500, height: 1491)
        theSprite.position = CGPoint(x: basePos.x+1, y: basePos.y+2)
        superScene.addChild(theSprite)

when clicked it runs this func and as I read here https://developer.apple.com/reference/spritekit/skaction/1417810-animate I have restore set to TRUE so it SHOULD end on the last frame of the array:

 let anim = SKAction.animate(with: atlas, timePerFrame: animSpeed, resize: false, restore: true)
            theSprite.run(anim, completion: {() -> Void in

            })

It should go back to normal texture (which is a blank image), but it doesn't. The only way it goes back to this is if I don't set the texture to begin with, which I need to do. How can I make it restore?


Solution

  • Suggested Solutions

    Try setting your sprite to the last texture in the atlas like this: theSprite.texture = atlas[atlas.count-1]. Then run your code again. You should see that it restores to back to atlas[atlas.count-1].

    If your animation stops at a texture other than your last atlas texture, then you could try this. Set theSprite.texture = atlas[atlas.count-1] within your animation's completion block. You would also set restore to false in this case. This will animate your sprite then set the final texture to whatever you want, in this case it will be set to the last atlas texture.

    Explanation

    Restore will return your sprite back to the texture that it had before the animation began. So in your case with your current code, it should end the animation with the texture atlas[0]. It is not meant to end on the last frame of the array as you stated.

    According to the Apple dev reference, if restore is true:

    When the action completes, the sprite’s texture is restored to the texture it had before the action completed. https://developer.apple.com/reference/spritekit/skaction/1417810-animate