iosbuttoncocos2d-iphoneclickccmenuitem

Making a button-click exclusive to the picture of the button?


I'm having an odd, irritating issue.

For example, in my main menu screen, I have a button that says "Instructions."

Once I click that, in the instructions layer, there is a button that takes you back to the main menu.

However, for some reason, the button action is not exclusive to the sprite image. If i click 3 inches away from the 'backtomenu' button, it still takes me back to the main menu.

So, my question is, how can I make a button be clicked only if you click the actual image? (this is how I create a button)

    - (id) init
{
    if((self = [super init]))
    {
        [self instructions];
    }
    return self;
}

- (void) instructions
{
    bgI = [CCSprite spriteWithFile:@"testbackground11.png"];
    [bgI setPosition:ccp(160,240)];
    ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
    [bgI.texture setTexParameters:&params];
    [self addChild:bgI z:0];

    returnToMenu = [CCMenuItemImage itemFromNormalImage:@"berry2.png"
                                                            selectedImage:@"berry2_selected.png"
                                                            target : self
                                                            selector: @selector (ifReturnToMenu:)];
    CCMenu *myReturnMenu = [CCMenu menuWithItems:returnToMenu, nil];

                            [myReturnMenu alignItemsVertically];

                            [self addChild: myReturnMenu];

}

- (void) ifReturnToMenu: (CCMenuItem *) menuItem
{
    if(menuItem == returnToMenu)
    [[CCDirector sharedDirector] replaceScene:
     [CCTransitionFade transitionWithDuration:0.5f scene: [MainMenu scene]]];
}

Solution

  • I am not sure how 'isReturnToMenu' is fired, but you can try this

    - (void) ifReturnToMenu: (CCMenuItem *) menuItem{
    if(menuitem == returnToMenu){
        [[CCDirector sharedDirector] replaceScene:
        [CCTransitionFade transitionWithDuration:0.5f scene: [MainMenu scene]]];
    }
    

    }

    If it doesnt work, you'll need to post the code that fires it so we can help you more