I have a problem getting a texture atlas. I currently have a SpriteKit game where a player can change his character. Right now I have an atlas in a global shared instance, for example.
let char0Atlas: SKTextureAtlas = SKTextureAtlas(named: "Atlas/iPhone/char0") let char1Atlas: SKTextureAtlas = SKTextureAtlas(named: "Atlas/iPhone/char1") let char2Atlas: SKTextureAtlas = SKTextureAtlas(named: "Atlas/iPhone/char2")
Each character you can change has its own atlas. I get the player moving, idle and transition animation from the atlas of characters using this method:
func charAnimation(char: CharEnum) -> [SKTexture] { var temp: [SKTexture] = [] var name: String = "" let atlas: SKTextureAtlas = char.atlasForChar() for i in 1...20 { if i > 9 { name = "char_\(charId)_idle_00\(i)" } else { name = "char_\(charId)_idle_000\(i)" } temp.append(atlas.textureNamed(name)) } return temp }
And this is stored in the array instance variable in the node sprite class. Thus, every time a character changes, these frames are replaced with new frames, so should the old ones be freed correctly?
class PlayerNode: SpriteNode { private var currentAnimation: [SKTexture] = [] private var animations: (idle: [SKTexture], run: [SKTexture], jump: [SKTexture]) = PlayerController.animationsForHero(.CharOne) }
In addition, when the player switches the characters, I use this to preload the texture atlas:
SKTextureAtlas.preloadTextureAtlases([char.atlasForHero()], withCompletionHandler: { () -> Void in updateChar() })
Why does SpriteKit never free memory from previous character animations? If the player switches to new characters, the memory is constantly increasing and the application crashes. If a character that has already been selected in this session is selected again, the memory does not increase. This indicates a memory leak. Character animations are not freed. Why?
I understand that SpriteKit must take care of this myself, so this is confusing. Is it impossible to manually delete the texture atlas on my own?
Thanks!