IPhone cocos2d sprites in an array, memory problems

I try to track my sprites in an array, add and remove them from layers, and then permanently clear them from the array.

I am using the following code:

Sprite * Trees[50];
Layer * Forest;

Forest =  [Layer node];
Forest.isTouchEnabled = YES;
[self addChild:Forest z:30];

// do this a bunch of times
Trees[0] = [[Sprite spriteWithFile:@"mytree.png"] retain];
[Trees[0] setPosition:cpv(240,160)];
[Forest addChild:Trees[0] z:5];

And then when I want to destroy the tree, I use:

[Forest removeChild:Trees[0] cleanup:YES];
[Trees[0] release];

My problem is that when I look in the "Tools", I never come back, this memory never falls. I thought that by freeing the sprite, it would free up memory. Am I doing this completely wrong?

+3
source share
3 answers

I know that when you use the simulator with cocos2d, the memory is not like releasing it, so you need to run it on the device in order to get an accurate picture of what is happening.

cocos2d .

, , , , , :

[[TextureMgr sharedTextureMgr] removeAllTextures]; 

.

:

Sprite * sPopup = [[Sprite spriteWithFile:@"popup.png"] retain];
    sPopup.position = cpv(240,440);
    [self addChild: sPopup z:2];
[sPopup release];

, sPopup , :

[[TextureMgr sharedTextureMgr] removeAllTextures]; 

.

+4

, "":

Trees[0] = [[Sprite spriteWithFile:@"mytree.png"] retain];

, , spriteWithFile Sprite .

, apple documentation, . , , . , (, ), , ( , ).

, , ( 0), , , .

, .

+3

[Trees [x] release], , "" , Trees [x] = nil - , .

"" , [Forest addChild: z:] (afaik).

0
source

Source: https://habr.com/ru/post/1705128/


All Articles