简体   繁体   中英

ccmenuitem from another layer cocos2d

I've seen an example on cocos2d forum with accessing sprites from different layer but tried and it doesn't working; maybe I'm not doing something as I should

For the scene method I have:

+(CCScene *)scene {

    CCScene *scene = [CCScene node];
    CurrentLayer *_currentLayer = [CurrentLayer node];
    aLayer *_aLayer = [aLayer node];
    [scene addChild: _currentLayer z:0 tag: 128];
    [scene addChild: _aLayer z:1 tag:288];

return scene;

}

I'm trying to access a CCMenuItem from aLayer in CurrentLayer , the way I do is:

    CCMenuItem *menuItemToBeAccessed = (CCMenuItem *)[[[self parent] getChildByTag:288] getChildByTag:60];

In aLayer I've placed my CCMenuItem in this way:

        menuItem = [[CCMenuItemSprite
                     itemFromNormalSprite:[CCSprite spriteWithSpriteFrameName:@"menuItemImage.png"]
                     selectedSprite:[CCSprite spriteWithSpriteFrameName:@"menuItemImage.png"]
                     target:self
                     selector:@selector(methodToBeCalled:)] retain];
        // the reason that I'm keeping my menu item disabled is that I want to enable when I press another ccmenuitem in the other layer
        menuItem.isEnabled = NO;
        menuItem.tag = kMenuItem; // which is added in enum as:  kMenuItem = 60
        [menuItem setAnchorPoint: ccp(0,0)];
        menuObject = [CCMenu menuWithItems:menuItem, nil];
        [menuObject setPosition: ccp(menuObject_X, menuObject_Y)];
        [self addChild:menuObject z:5];

How the path to my CCMenuItem would look like?

UPDATE:

GamePlay->ParallaxLayers->aLayer
     access bLayer from aLayer

You've pointed me to:

CCMenuItem *bCCMenuItem = (CCMenuItem *)[(CCMenu *)[[self.parent getChildByTag:kLayerTag] getChildByTag:kMenuTag] getChildByTag:kMenuItemTag];


GamePlay->ParallaxLayers->bLayer
     access aLayer from bLayer

Despite any facts about the way you're trying to access a menu item from another layer, you're missing the fact that your CCMenuItem is a child of your CCMenu.

You would need to access the CCMenu and then, access it's child CCMenuItem by tag.

Something like:

CCMenuItem *toBeAccessed = (CCMenuItem *)[(CCMenu *)[[self.parent getChildByTag:kLayerTag] getChildByTag:kMenuTag] getChildByTag:kMenuItemTag];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM