How the property contains a link to child nodes with ARC in Cocos2d

I have a custom class CCNodethat has a bunch of child nodes, and I want to keep references to the child elements in order to make some custom transitions.

For example, for a child background, a custom class would look like this:

@interface MyNode : CCNode
@property (nonatomic, strong) CCNode *background;
@end

@implementation
- (void)setBackground:(CCNode *)background {
    if (_background) {
        [self removeChild:_background];
    }
    if (background) {
        [self addChild:background];
    }
    _background = background;
}
- (void)runTransition {
    if (_background)
        [_background runAction:[…]];
}
@end

The problem is that this causes a save loop on the ARC with a node background that is never freed from memory.

+4
source share
2 answers

There are no hard and fast rules for managing memory. You need to look at your code and choose the best way to use it.

, , , . Cocos2d , , .

, node , , node , .

+1

, :

@interface MyNode : CCNode
@property (nonatomic, weak) CCNode *background;
@end

node , _background ivar . , . , node node, node, .

.

, , , , nil node :

_background = [CCNode node];
[self addChild:_background];

, node, nil addChild:

:

CCNode* bg = [CCNode node];
[self addChild:bg];
_background = bg;

, node , children node. , addChild: bg node _background ivar.

+1

All Articles