2012-01-09 66 views
0

我試圖改變對飛這樣我CCMenuItemImage的圖像:(Cocos2D)子項已添加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' 

難道我做錯了什麼這裏?

謝謝!

回答

2

好吧,setNormalImage後,newSprite對象有一個父對象(imageItem對象)。當您使用SAME newSprite對象設置DisabledImage時,coco可能會發出嗚嗚聲,因爲sprite對象已經位於某個CCNode層次結構中(即nil!= newSprite.parent)。

您可能想要創建一個newSpriteEnabled對象和一個newSpriteDisabled對象,然後將顏色設置爲newSpriteDisabled並從此處取出。