0

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]]];
}
Surz
  • 984
  • 3
  • 11
  • 36
  • 3
    Is there some extra transparent padding on the image? The button tap area is based on the size of the sprite. – Ben Trengrove Sep 04 '12 at 04:31
  • I can't see anything wrong with your code. Have you overriden onEnter or something similar and forgotten to call the super? – Ben Trengrove Sep 05 '12 at 05:55
  • I redid the entire button/menu with the same images and code and it worked! Maybe I should of just cleaned the project. – Surz Sep 05 '12 at 06:45

1 Answers1

1

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

yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • Hmm. still having the same 'error'. (I click the bottom of the screen and the button still performs its action) – Surz Sep 04 '12 at 05:40
  • Your code seems ok, so I'm with @Ben on this one, it could be the image's extra transparent space. Try cropping out the extra space if there is any. – yannicuLar Sep 04 '12 at 21:04