I try to clone a CCNode hierarchy, the problems is I need to reset and set all the variable by type, can I have a way to do that more automatically ?
basiclly what I want to do is : - store a CCNode* (with some child, for example an image at Pos 10-10, and a Label at Pos 100-50 with the text "Test"); - then clone it, for get a new CCNode* with the same default value and childs.
I need to copy it, because after they will get modify, is like a template of Node, before get custom value.
If you know a simple way to copy, and set all the hierarchy (with correct type also), without big if/else statement for each kind of type, it will help me a lot ^^
thanks
This code clones CCNode and all child CCNodes recursively. You can add other subclasses and other properties to copy.
+ (CCNode*) cloneCCNode:(CCNode*)source
{
CCNode* clone = [CCNode node];
for (CCNode* srcSubnode in source.children) {
CCNode* subnode;
if ([srcSubnode isKindOfClass:[CCSprite class]]) { //only CCSprites are copied, add other subclasses if you need to
CCSprite* srcSprite = (CCSprite*)srcSubnode;
subnode = [CCSprite spriteWithTexture:srcSprite.texture];
((CCSprite*)subnode).displayFrame = srcSprite.displayFrame;
} else {
subnode = [self cloneCCNode:srcSubnode];
}
subnode.rotation = srcSubnode.rotation;
subnode.position = srcSubnode.position;
subnode.anchorPoint = srcSubnode.anchorPoint;
subnode.zOrder = srcSubnode.zOrder;
[clone addChild:subnode];
}
return clone;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With