I have a scene, a main layer and a child layer, the main layer contains a Player* object which is a subclass of CCSprite, I want to pass the Player into the child layer like so:
BattleLayer* b = [[BattleLayer alloc] initWithPlayer:_player];
[((CCScene*)self.parent) addChild:b];
-(id)initWithPlayer:(Player *)p
{
if((self=[super init]))
{
_player = p;
[self addChild:_player.spriteSheet];
}
return self;
}
However, this gives an error when I do [self addChild:_player.spriteSheet];
*** Assertion failure in -[BattleLayer addChild:z:tag:]
I’ve tried removing the sprite from the main layer before passing it in, but even with cleanup:NO this gives an error of
*** -[CCSpriteBatchNode tag]: message sent to deallocated instance 0x747a580
What am I doing wrong here? And what’s the best way of achieving this?
I’ve run in to this before and retained and then released after reordering. My objects were autoreleased and retained only by the CCLayer so this worked, Try:
However I have run into a bug with rapid removal and re-adding of sprites and it is described here: http://www.cocos2d-iphone.org/forum/topic/9980