我在cocos2d论坛上看到了一个example来访问来自不同层的精灵,但是尝试了它并没有起作用;也许我没有做我应该做的事情
对于场景方法,我有:
+(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
答案 0 :(得分:2)
尽管您尝试从其他图层访问菜单项的方式有任何事实,但您错过了您的CCMenuItem是CCMenu的孩子这一事实。
您需要访问CCMenu,然后按标签访问它的子CCMenuItem。
类似的东西:
CCMenuItem *toBeAccessed = (CCMenuItem *)[(CCMenu *)[[self.parent getChildByTag:kLayerTag] getChildByTag:kMenuTag] getChildByTag:kMenuItemTag];