swiftanimationskactionsktextureatlas

How do I use merged images for animation with Swift?


I did a simple animation with a total of four images in a graphical editor. When I exported the images, they ended up in a single png. Not sure why I didn't just get four separate images - those would be easy to use. Anyway, how do I loop the all-in-one image for an animation? Or how do I separate the images to individual png files?

This is how I would go about animate the images if they were separate:

let textureAtlas = SKTextureAtlas(named: "Character")

    for item in 1...textureAtlas.textureNames.count {
        let texturenames = "character" + String(item)

        characterTexture.append(textureAtlas.textureNamed(texturenames))
    }

    characterSpriteNode.run(SKAction.repeatForever(SKAction.animate(with: characterTexture, timePerFrame: 0.5)))

Solution

  • You can't use an APNG for a texture. I agree with this similar question that the best thing to do is just to split them into separate PNGs.

    SKSpriteNode using APNG for texture

    Whatever software you used to create the original should have an option to export as individual PNGs. Every application is different but in Photoshop you use "Save for web" and in Procreate you can use "Share Layers > PNG Files" instead of "Share Layers > Animated PNG".