I’m adding two CCMenuItemImage objects to a CCMenuItemToggle like so:
CCMenuItemImage *soundEnabled = [CCMenuItemImage itemWithNormalImage:@"button_sound_enabled.png"
selectedImage:@"button_sound_enabled.png"];
soundEnabled.tag = kSoundEnabled;
CCMenuItemImage *soundDisabled = [CCMenuItemImage itemWithNormalImage:@"button_sound_disabled.png"
selectedImage:@"button_sound_disabled.png"];
soundDisabled .tag = kSoundDisabled;
CCMenuItemToggle *sound = [CCMenuItemToggle itemWithItems:[NSArray arrayWithObjects:soundEnabled,soundDisabled,nil] block:^(id sender) {
CCMenuItem *item= ((CCMenuItemToggle*).sender).selectedItem;
CCLog(@"item tag: %d",item.tag);
}];
kSoundEnabled and kSoundDisabled are enumeration items with the values 2 and 3. When I log the tag of each CCMenuItemImage after i created them, everything is fine. But when I log them inside the block, the tags show up as -1061138431.
Also when I try to log them outside of the block, just a but further down in the init code of my layer they start to turn up wrong.
Does anybody know what the issue is here? It’s a Kobold2d ARC-enabled project, could ARC be the issue here? I thought this wouldn’t account for simple datatypes like NSInteger?
I know I could just check for sender.selectedIndex = 0 or sender.selectedIndex = 1 but I’d still like to understand what’s the issue here.
The issue does not come from the ARC configuration or others. I tested it with Cocos2D 2.0 and I have the same issue. I checked the sources and the problem comes from the CCMenuItemToggle which change the tag of the children to keep the track of the current display item.
I should you to use the reference of your variable into your block like that: