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]];