1

I make a SpriteKit game and facing many problem in Memory management due to large number of animations and Images

I know that the SKSpriteNode is Object to be displayed on the Screen, and SKTexture is the memory representation of images. How many SkSpriteNode(s) can share one SKTexture to decrease memory pressure?

In an attempt to lesson pressure, I made Singleton Class which loads All Texture and all Animations.

+ (GameTextureLoader *)sharedTextures{
static dispatch_once_t onceToken;
static id sharedInstance;
dispatch_once(&onceToken, ^{
    sharedInstance = [[self alloc] init];
});
 return sharedInstance;
}

...

SKSpriteNode *RedBird = [SKSpriteNode spriteNodeWithTexture:[self.loader.RedBird_LVL1 objectAtIndex:0]];

and then use them in the Game but each screen i open the Memory is increasing i think it should be constant after loading all texture and animation

btw i decrease all images BitDepth and use texture output format RGBA4444_compressed

any suggestion please ?

here is how i load Texture in NSMutableArray with Strong Pointer

SKTextureAtlas *RedBirdAtlas = [SKTextureAtlas atlasNamed:@"RedBird-iPad"]; // don't put .atlas in there
    [SKTextureAtlas preloadTextureAtlases:@[RedBirdAtlas] withCompletionHandler:^{
        SKTexture *RedBird1 = [RedBirdAtlas textureNamed:@"RedBird_01_iPad.png"];
        SKTexture *RedBird2 = [RedBirdAtlas textureNamed:@"RedBird_02_iPad.png"];
        SKTexture *RedBird3 = [RedBirdAtlas textureNamed:@"RedBird_03_iPad.png"];

        [self.RedBird_LVL1 addObject:RedBird1];
        [self.RedBird_LVL1 addObject:RedBird2];
        [self.RedBird_LVL1 addObject:RedBird3];
        finishTextureCount++;
        [self checkfinishLoading];
    }];

and i Call it in Skcene like this:

SKSpriteNode *RedBird = [SKSpriteNode spriteNodeWithTexture:[self.loader.RedBird_LVL1 objectAtIndex:0]];
    RedBird.position = RedBirdPoint;
    RedBird.name = @"RedBird";

and use it in animation Like this :

SKAction* atlasAnimation = [SKAction animateWithTextures:[self.loader.RedBird_LVL1 objectAtIndex:0] timePerFrame:Cloud_Animation];

SKAction *resetTexture = [SKAction setTexture:[self.loader.RedBird_LVL1 objectAtIndex:2]];
_RedBirdAnimation = [SKAction sequence:@[atlasAnimation,resetTexture]];
khaled
  • 865
  • 9
  • 24
  • 1
    Post the code where you actually load the textures. Also, are you using texture atlas? If not, do so. – CodeSmile Apr 02 '14 at 13:36
  • yes i use texture Atlas – khaled Apr 02 '14 at 22:58
  • First off, do not use a singleton. That will just ensure that you will always leak texture memory. Let SpriteKit manage the texture atlas memory for you, just be aware that use of an atlas is not always the best approach. You can also save texture memory by decompressing and rendering at the same time, as described here: http://stackoverflow.com/a/38679128/763355 – MoDJ Jul 31 '16 at 22:25

0 Answers0