CCMenuItemSprite CCLabelBMFont选择器无法正常工作

时间:2013-11-25 04:32:40

标签: menu cocos2d-iphone selector

我在下面的代码中使选择器工作时遇到问题。甚至日志也不会显示任何内容。我该如何解决这个问题?

-(void)displaySettingsMenuItems {

CCSprite *buttonHolder = [CCSprite spriteWithFile:@"buttonHolder.png"];
buttonHolder1.position = ccp(screenSize.width/2, screenSize.height/2);
[self addChild:buttonHolder z:ZPos];
CCMenuItemSprite *instructionsItemSprite = [CCMenuItemSprite itemWithTarget:self selector:nil];

CCLabelBMFont *instructionsLabelFont = [CCLabelBMFont labelWithString:@"instructions" fntFile:@"TestingFont.fnt"];
CCMenuItemLabel *instructionsItemLabel = [CCMenuItemLabel itemWithLabel:instructionsLabelFont target:self selector:@selector(instructionsLayer)];

instructionsLabelFont.position =ccp(screenSize.width/2, 0.99*screenSize.height/2);
[instructionsItemSprite addChild: instructionsItemLabel z:ZPos];
[self addChild:instructionsItemSprite z:ZPos];
}

选择器的方法:

-(void)instructionsLayer {
CCLOG(@"code is okay");
[[MenuManager sharedMenuManager] runWithPrePlayMenu:kInstructions];
}

1 个答案:

答案 0 :(得分:0)

您应该将您的instructionsItemLabel菜单项添加到CCMenu(而不是CCMenuItem),然后将菜单添加到场景中。 CCMenu是菜单项(一个或多个)的占位符,并扩展了CCLayer类。它为嵌入的菜单项提供触摸处理,并在存在涉及特定菜单项的触摸事件时调用其菜单项。

-(void)displaySettingsMenuItems {

    CCSprite *buttonHolder = [CCSprite spriteWithFile:@"buttonHolder.png"];
    buttonHolder.position = ccp(screenSize.width/2, screenSize.height/2);  // <- you had buttonHolder1 ivar 
    [self addChild:buttonHolder z:zPos];

    CCLabelBMFont *instructionsLabelFont = 
        [CCLabelBMFont labelWithString:@"instructions" fntFile:@"TestingFont.fnt"]; 
    CCMenuItemLabel *instructionsItemLabel = 
        [CCMenuItemLabel itemWithLabel:instructionsLabelFont 
                                target:self 
                              selector:@selector(instructionsLayer)];

    CCMenu *menu = [CCMenu menuWithTtems:instructionsItemLabel,nil];
    menu.position = buttonHolder.position;
    [self addChild:menu];
}