我试图像这样动态更改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'
我在这里做错什么了吗?
谢谢!
最佳答案
好了,在设置NormalImage之后,newSprite对象具有一个父对象(imageItem对象)。当您使用SAME newSprite对象设置setDisabledImage时,coco可能会抱怨,因为sprite对象已经在某个CCNode层次结构中(即nil!= newSprite.parent)。
您可能要创建一个newSpriteEnabled对象和一个newSpriteDisabled对象,然后将颜色设置为newSpriteDisabled,然后从那里获取它。
关于ios - (Cocos2D)子级已经添加CCMenuItemImage吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8782816/