1
votes

By pre-setting up strong SKActions for all my in game animations is the actual image data cached by Sprite Kit for future use. What I am trying to understand is if I setup all my animation as retained SKActions is there any point in also preloading the SKTextureAtlas that they originally came from, my thinking is no as the images for the SKActions are already loaded in memory?

for(NSUInteger index=1; index<=15; index++) {
    NSString *image = [NSString stringWithFormat:@"Drone_FLYING_%04ld", (long)index];
    SKTexture *texture = [SKTexture textureWithImageNamed:image];
    [[self  framesDroneFlying] addObject:texture];
}

[self setDroneFlyAction:[SKAction animateWithTextures:[self framesDroneFlying]          timePerFrame:DRONE_FLY_SPEED]];

I am guessing at this from the Apple Adventure Example, which although the authors use lots of SKTextureAtlas(es) they don't do a single preloadTextureAtlases in any of the code.

1
SK will load textures on the fly. However preloading the SKActions seems like taking it too far, they're lightweight objects that can be created on the fly and I wager getting them from storage will be about if not more expensive than creating a new action for every animation. Take the easier approach until you've proven with actual measurements that it does make a difference AND your app is impacted by it. Otherwise it's premature optimization. - LearnCocos2D
but you don't want textures loading on the fly the first time that ship_x enters the screen, what I am trying to get is the best approach to pre setting up assets so you don't take a hit mid game. - fuzzygoat
Use SKTextureAtlas preload method and keep a strong reference to the atlas object. That'll keep all textures in the atlas in memory. You can leave the above code unchanged and instead of loading the image it will look in the atlas - provided that you set up the atlas correctly (ie .atlas folder with all resources as reference instead of group, and not adding image files with the same name elsewhere to the project). - LearnCocos2D
Thats a very good explanation, something that I have been curious about for a while and have not found any definitive answer, you should add your comment as the answer I am sure a lot of other people will be helped by knowing this ... Much appreciated. - fuzzygoat
Just as aside note for anyone wondering, reference folders in Xcode are BLUE, group folders are YELLOW. - fuzzygoat

1 Answers

1
votes

To keep textures (of an atlas) cached, use SKTextureAtlas preload method and keep a strong reference to the atlas object. That is all. That'll keep all textures in the atlas in memory.

You can leave the above code unchanged and instead of loading the image it will look in the atlas - provided that you set up the atlas correctly (ie .atlas folder with all resources as reference instead of group, and not adding image files with the same name elsewhere to the project).