AJak
AJak

Reputation: 3873

CCMenuItemImage - setSelectedImage and setNormal Image

Ran across this while working with CCMenuItemImage. It seems like I have to set the SelectedImage and the NormalImage to different CCSprites, otherwise it crashes my application. (I plan on using unique assets later on for both states) CCMenuItemImage *ItemButton; is defined / initialized.

The following does not work:

CCSprite *updatedSprite = [CCSprite spriteWithFile:@"1_button.png"];
[ItemButton setNormalImage:updatedSprite];
[ItemButton setSelectedImage:updatedSprite];

The following does work:

CCSprite *updatedSpriteNormal = [CCSprite spriteWithFile:@"1_button.png"];
[ItemButton setNormalImage:updatedSpriteNormal];

CCSprite *updatedSpriteSelected = [CCSprite spriteWithFile:@"1_button.png"];
[ItemButton setSelectedImage:updatedSpriteSelected];

Curious to know why that would happen, I've done some digging but couldn't find anything definitive. Any insight would be great.

Upvotes: 0

Views: 1274

Answers (1)

YvesLeBorg
YvesLeBorg

Reputation: 9089

When you setSelectedImage, the sprite is added as a child to the ItemButton, thus it has a parent. You must create a second instance of CCSprite to setNormalImage, because the node hierarchy of cocos2d will always prevent adding as a child an object that already has a parent.

Upvotes: 2

Related Questions