Cocos2d - 带有多个按钮的CCMenu在第一次加载时调用错误的选择器

时间:2012-02-01 14:05:23

标签: iphone objective-c cocos2d-iphone

这是一个有趣的困境。我在页面上加载了两个CCMenus,每个CCMenus都有两个CCMenuItemImages作为按钮。所有四个按钮都调用相同的函数,该函数决定使用一个脱离调用者标记值的switch语句做什么。

这四个按钮分别是开始,教程,选项和学分。我将它们分成两个菜单,这样我就可以在人造网格中水平和垂直对齐它们。这个图层是主菜单图层,所以它是游戏开始后首先加载的。

问题在于,当游戏首次加载时,按任何按钮将调用“选项”按钮。不仅仅是功能,按菜单上的任何按钮激活选项按钮的选定状态。例如,如果我按“开始”,开始按钮的选定状态(图像周围的光晕)不起作用 - 选项按钮会发光。

进入选项菜单然后退出后,主菜单按预期工作,每个按钮激活其必需功能。

我应该注意到我还运行了一个干净的应用程序,并从模拟器和我的iPhone中移除了应用程序并重建了它。

这是我的.h:

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Constants.h"
#import "GameManager.h"

@interface MainMenuLayer : CCLayer {
    CCMenu *mainMenuTop;
    CCMenu *mainMenuBottom;
}
@end

这是我的.m:

#import "MainMenuLayer.h"

// Private methods
@interface MainMenuLayer()
- (void)displayMainMenu;
@end

@implementation MainMenuLayer

- (void)playScene:(CCMenuItemFont*) itemPassedIn {
    if ([itemPassedIn tag] == 1) {
        CCLOG(@"Tag 1 found, Scene 1");
        [[GameManager sharedGameManager] runSceneWithID:kGameplayScene];
    } else if ([itemPassedIn tag] == 2) {
        CCLOG(@"Tag was: %d", [itemPassedIn tag]);
        CCLOG(@"Placeholder for next chapters");
    } else if ([itemPassedIn tag] == 3) {
        CCLOG(@"Tag 3, Options");
        [[GameManager sharedGameManager] runSceneWithID:kOptionsScene]; 
    } else if ([itemPassedIn tag] == 4) {
        CCLOG(@"Tag 4, Credits");        
        [[GameManager sharedGameManager] runSceneWithID:kCreditsScene];
    }
}

- (void)displayMainMenu {
    CGSize winSize = [CCDirector sharedDirector].winSize;

    // Main Menu Top Layer Buttons
    CCMenuItemImage *playGameButton = [CCMenuItemImage itemFromNormalImage:@"button-start-up.png" selectedImage:@"button-start-down.png" disabledImage:nil target:self selector:@selector(playScene:)];
    [playGameButton setTag:1];

    CCMenuItemImage *tutorialButton = [CCMenuItemImage itemFromNormalImage:@"button-tutorial-up.png" selectedImage:@"button-tutorial-down.png" disabledImage:nil target:self selector:@selector(playScene:)];
    [tutorialButton setTag:2];

    // Main Menu Bottom Layer Buttons
    CCMenuItemImage *optionsButton = [CCMenuItemImage itemFromNormalImage:@"button-options-up.png" selectedImage:@"button-options-down.png" disabledImage:nil target:self selector:@selector(playScene:)];
    [optionsButton setTag:3];

    CCMenuItemImage *creditsButton = [CCMenuItemImage itemFromNormalImage:@"button-credits-up.png" selectedImage:@"button-credits-down.png" disabledImage:nil target:self selector:@selector(playScene:)];
    [creditsButton setTag:4];

    mainMenuTop = [CCMenu menuWithItems:playGameButton,tutorialButton,nil];
    mainMenuBottom = [CCMenu menuWithItems:optionsButton,creditsButton,nil];

    [mainMenuTop alignItemsHorizontallyWithPadding: 10.0f];
    [mainMenuTop setPosition: ccp(winSize.width/2, -500)];
    [mainMenuBottom alignItemsHorizontallyWithPadding:10.0f];
    [mainMenuBottom setPosition:ccp(winSize.width/2, -600)];

    id moveActionTop = [CCMoveTo actionWithDuration:0.5f position:ccp(winSize.width/2, 150)];
    id moveEffectTop = [CCEaseIn actionWithAction:moveActionTop rate:1.0f];
    [mainMenuTop runAction:moveEffectTop];
    [self addChild:mainMenuTop z:2 tag:kMainMenuTagValue];

    id moveActionBottom = [CCMoveTo actionWithDuration:0.5f position:ccp(winSize.width/2, 75)];
    id moveEffectBottom = [CCEaseIn actionWithAction:moveActionBottom rate:1.0f];
    [mainMenuBottom runAction:moveEffectBottom];
    [self addChild:mainMenuBottom z:3 tag:kMainMenuBottomTagValue];
}

-(id)init {
    self = [super init];
    if (self != nil) {        
        [self displayMainMenu];
    }
    return self;
}

@end

1 个答案:

答案 0 :(得分:0)

我最后只是手动定位。我尝试使用LearnCocos2D链接的方法,但我无法确定如何使其正常工作;这些物品并没有以适当的网格结束。