Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
584 views
in Technique[技术] by (71.8m points)

sprite kit - SKTextureAtlas Loading and Memory Management

I developing a Game on SpriteKit and have Multiple Scenes each Scene has From 3 TextureAtlas in Minimum and Maximum Size of an Image in Each TextureAtlas is 60K my game Crashes From Memory issue

what I do in Each Scene is Defining action in the Header File for Example:

initialise them in -(id)initWithSize:(CGSize)size  Function 


@interface FirstLevel : SKScene
{

    SKAction  *RedBirdAnimation;
}

and in 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;}

is there Something Like Best Practice to Load Texture Atlas in my game to prevent it from Crashes due to Memory.

i make all SkAction with Nil at the end of Each Skscene and remove All action from All SkSpriteNode

is there any solution

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Each TextureAtlas is 60K

As a file maybe. But that's not memory usage. To calculate memory usage of an image file, take the file's dimensions and multiply them with color bit depth (usually 32 bits = 4 bytes).

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

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

Long story short: use Instruments to verify your app's actual memory consumption.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...