Moving sprite from 1 layer to another.

Hi,

I have a situation where I would like to move some sprites from 1 layer to another.

I’m wondering what the correct way to do this would be (if it can be done)?
Can I just do something like this:-

CCArray* items = getAllSprites();
CCObject* item;
CCARRAY_FOREACH(items, item){
layer1~~>removeChild;
layer2~~>addChild(item);
}

Hi,

Any ideas how I might go about moving a sprite from 1 layer to another?

The code I tried in my first post does not work.

Just doing removeChild does not seem to actually work as all the removed sprites still get rendered as normal.

Please try

CCArray* items = layer1->getChildren();
CCObject* item;
CCARRAY_FOREACH(items, item)
{
    CCNode* node = (CCNode*)item;
    layer1->removeChild(node);
    layer2->addChild(node);
}

That rebooted my phone!

layer1~~>removeChild does not exist so I used layer1~~>removeChild(node, false).

The removeChild call does not seem to have any effect by itself as everything still appears after.

Removing the addChild call stops the crashing.

I cast my sprite to CCNode* and both layers to CCLayer* but still no joy.

Maybe the sprite is not in layer1 so the removeChild is failing.

Adding the same node to the other layer causes the crash as you cant have a node on 2 layers.

I’m using a level creation / loading tool so it might be putting it on another layer.
I’ll check the code and see what leyer the sprite is being added to.

Try it.
CCArray* items = layer1~~>getChildren;
CCObject* item;
CCARRAY_FOREACH
{
CCNode* node = item;
node~~>retain();
layer1~~>removeChild;
layer2~~>addChild(node);
node->release();
}

I tried the retain / release stuff but still have the same problem.

I think its maybe the fact that I’m using a framework that wrappers CCLayer and CCSprite.
Maybe its not setup to support the moving of sprites.

Thanks for everyones help.