Download and memory management SKTextureAtlas

I am developing a game on SpriteKit and have several scenes, each of which has From 3 TextureAtlas to Minimum and maximum image size in each texture. my game crashes from memory problem

what I do in each scene is the definition of the action in the header file for an example:

initialise them in -(id)initWithSize:(CGSize)size Function @interface FirstLevel : SKScene { SKAction *RedBirdAnimation; } 

and in the implementation file:

 -(id)initWithSize:(CGSize)size{ if(self=[super initWithSize:size]) {[self setupRedBirdActions];} return self; } -(void)setupRedBirdActions{ SKTextureAtlas *RedBirdAtlas = [SKTextureAtlas atlasNamed:@"RedBird"]; SKTexture *RedBird1 = [RedBirdAtlas textureNamed:@"Redbird_01_iphone.png"]; SKTexture *RedBird2 = [RedBirdAtlas textureNamed:@"Redbird_02_iphone.png"]; SKTexture *RedBird3 = [RedBirdAtlas textureNamed:@"Redbird_03_iphone.png"]; NSArray *atlasTexture = @[RedBird1, RedBird2, RedBird3]; SKAction* atlasAnimation = [SKAction animateWithTextures:atlasTexture timePerFrame:0.2]; RedBirdAnimation = atlasAnimation;} 

there is something like best practice for loading Atlas textures in my game to prevent it from crashing due to memory.

I do all SkAction with Nil at the end of each skzen and remove all actions from All SkSpriteNode

is there any solution

+2
source share
3 answers

Each TextureAtlas is 60K

How the file can be. But this is not memory usage. To calculate the memory usage of the image file, take the file sizes and multiply them by the color depth (usually 32 bits = 4 bytes).

For example, a 4096x4096 texture uses 16 MB of texture memory (but it can be much less than 1 MB as a PNG file).

 4096 x 4096 x (32/8) = 16 Megabytes 

In short: use the Tools to check the actual memory consumption in your application.

+2
source

The problem with a SpriteKit-based game ending in memory is almost always caused by a developer using so many textures that all active RAM is consumed by textures. I'm going to assume that you are using a standard SKAction-based animation approach and that you don't have any weird memory leaks caused by strong links or anything else. The correct way to determine the amount of RAM consumed by a texture (WIDTH * HEIGHT * 4/1000) indicates the amount of KB of memory needed to store the texture. For texture 4096x4096, which is 67108 kB or about 68 megabytes. It does not matter whether it is stored in a satin or as a single texture. The key to actually reducing the total amount of memory used in SpriteKit is to reduce the amount of memory consumed by each texture. Please see the source code for this SpriteKitFireAnimation example , which shows how reducing the memory usage in each frame can lead to a reduction from 286 megabytes to 130 megabytes for a very complex alpha channel animation that runs at 60FPS on a 64-bit iOS system. This approach is intended only for A7 and newer 64-bit systems. If you're looking for better, but completely free approaches, see this comparison of very compressed approaches for texture compression with SpriteKit. As the last effort in the ditch, you can reduce the width and height of each texture by 1/2, and then scale the textures when rendering in the node, this does not look so good, but uses 4x less memory at runtime.

+2
source

TextAtlas is too large to boot the system. You need to split the images into two or more separate texture textures, allowing the system to manage memory. Put the images you need for initial setup in your own atlas so you can preload them at the beginning of the game. Access images as if they were separate files, and the system will load and unload atlases as needed. The system can do better with memory management, so you can enable it.

0
source

All Articles