Reputation: 12444
I am trying to change the image of my CCMenuItemImage on the fly like this:
- (void)playOrPauseMusic {
MPMusicPlayerController *musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
CCSprite *newSprite;
if (itemImage.tag == 50) {
[musicPlayer pause];
newSprite = [CCSprite spriteWithFile:@"Image1.png"];
playstopButton.tag = 51;
} else {
[musicPlayer play];
newSprite = [CCSprite spriteWithFile:@"Image2.png"];
itemImage.tag = 50;
}
CGPoint scale6 = CGPointMake(164 / newSprite.contentSize.width,
48 / newSprite.contentSize.height);
[newSprite setScaleX:scale6.x];
[newSprite setScaleY:scale6.y];
[itemImage setNormalImage:newSprite];
newSprite.color = ccc3(128, 128, 128);
[itemImage setDisabledImage:newSprite];
}
The problem is, whenever this method gets called this crash occurs:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again'
Am I doing something wrong here?
Thanks!
Upvotes: 0
Views: 918
Reputation: 9089
well, after you setNormalImage, the newSprite object has a parent (the imageItem object). When you setDisabledImage with the SAME newSprite object, coco is probably whining because the sprite object is already in a CCNode hierarchy somewhere (ie nil!=newSprite.parent) .
You may want to create a newSpriteEnabled object and a newSpriteDisabled object, then set color to newSpriteDisabled, and take it from there.
Upvotes: 2