我在cocos2d论坛上看到了example,可以访问来自不同层的sprite,但是尝试了一下,但是它不起作用。也许我没有做我应该做的事

对于场景方法,我有:

+(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;

}


我正在尝试从CCMenuItem中的aLayer访问CurrentLayer,我的方法是:

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


在aLayer中,我以这种方式放置了CCMenuItem

        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];


CCMenuItem的路径如何?

更新:

GamePlay->ParallaxLayers->aLayer
     access bLayer from aLayer


您已指示我:

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


GamePlay->ParallaxLayers->bLayer
     access aLayer from bLayer

最佳答案

尽管您尝试从另一层访问菜单项的方式有任何事实,但您缺少CCMenuItem是CCMenu的子级的事实。

您需要访问CCMenu,然后通过标签访问它的子CCMenuItem。

就像是:

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

关于iphone - 来自另一层cocos2d的ccmenuitem,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9122335/

10-10 20:33