Note that there are some explanatory texts on larger screens.

plurals
  1. POCocos2d - CCMenu with multiple buttons calls wrong selector on first load
    primarykey
    data
    text
    <p>Here's an interesting dilemma. I have two CCMenus being loaded on a page, each with two CCMenuItemImages as buttons. All four buttons call the same function, which decides what to do using a switch statement that goes off the caller's tag value.</p> <p>The four buttons are start, tutorial, options, and credits. I have them split into two menus so that I can horizontally and vertically align them in a faux grid. This layer is the main menu layer, so it is the first thing to load after the game starts.</p> <p>The problem is that when the game first loads, pressing <em>any</em> button will call the "options" button. Not just the function, pressing any button on the menu <strong>activates the options button's selected state</strong>. If I press "start," for instance, the start button's selected state (a glow around the image) doesn't work–the options button glows instead.</p> <p>Once I get into the options menu, and then back out of it, the main menu works as expected, with each button activating its requisite function.</p> <p>I should note that I've also run a clean, and removed the app from both the simulator and my iPhone and rebuilt it.</p> <p>Here's my .h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #import "cocos2d.h" #import "Constants.h" #import "GameManager.h" @interface MainMenuLayer : CCLayer { CCMenu *mainMenuTop; CCMenu *mainMenuBottom; } @end </code></pre> <p>And this is my .m:</p> <pre><code>#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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload