我试图像这样动态更改CCMenuItemImage的图像:
- (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];
}
问题是,无论何时调用此方法,都会发生此崩溃:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'child already added. It can't be added again'
我在这里做错了吗?
谢谢!
答案 0 :(得分:2)
好吧,在setNormalImage之后,newSprite对象有一个父对象(imageItem对象)。当您使用SAME newSprite对象setDisabledImage时,coco可能正在抱怨,因为sprite对象已经位于某个CCNode层次结构中(即nil!= newSprite.parent)。
您可能想要创建一个newSpriteEnabled对象和一个newSpriteDisabled对象,然后将颜色设置为newSpriteDisabled,并从那里获取它。