0
votes

I am making an IOs game for an assignment using cocos2D. So far I am just trying to get all of the menus done, but I am having some issues with bounding box when setting up some of my buttons.

The game starts with the cocos2D logoscreen, then transits into a splash screen for my game, then into the main menu. In the game menu I have 2 buttons: Begin, and Options. Both buttons are working perfectly fine, each makinga transicion to the correct scene.

I am currently working on the options menu scene, where I have a button to take care of volume, a butto to set game difficulty, and a button to go back to main menu. I have been trying to make the back button work, but is not triggering when touched, I placed an NSLog for debugging, and the touches are being detected, just the if statement that checks whether the sprite was touched oris never true. I am puzzled by this, since I am using the same method I used in my main menu (which works). I searched for similar issues before posting, but everything I tried, like altering the parent coordinate system and/or the sprites boundingbox.origin, haven't worked at all.

This is the code for my Options Scene:

#import "OptionsMenu.h"
@implementation OptionsMenu
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    OptionsMenu *layer = [OptionsMenu node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{

    if( (self=[super init]) ) {
        self.touchEnabled = YES;
        self.accelerometerEnabled = YES;


        CGSize winSize = [[CCDirector sharedDirector] winSize];

        CCSprite *background = [CCSprite spriteWithFile:@"Menu0.png"];
        background.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:background z:0];

        CCSprite *volumeBar = [CCSprite spriteWithFile:@"VolumeBar.png"];
        volumeBar.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:volumeBar z:1];

        CCSprite *volumeButton = [CCSprite spriteWithFile:@"VolumeButton.png"];
        volumeButton.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:volumeButton z:1];

        CCSprite *difficultyButton = [CCSprite spriteWithFile:@"EasyButton.png"];
        difficultyButton.position = ccp(winSize.width/2, winSize.height/2-55);
        [self addChild:difficultyButton z:1];

        CCSprite *backButton = [CCSprite spriteWithFile:@"BackButton.png"];
        backButton.position = ccp(winSize.width/12, winSize.height/8);
        [self addChild:backButton z:1];

        [self scheduleUpdate];
    }

    return self;
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Add a new body/atlas sprite at the touched location
    for( UITouch *touch in touches ) {
        CGPoint location = [touch locationInView: [touch view]];
        location = [[CCDirector sharedDirector] convertToGL: location];

        CCActionInterval *actionInterval = [CCActionInterval actionWithDuration:0.1];


        if (CGRectContainsPoint([self.backButton boundingBox], location)) {
            [self.backButton setTexture:[[CCTextureCache sharedTextureCache] addImage:@"BackButton.png"]];
            id actionCallFunc = [CCCallFunc actionWithTarget:self selector:@selector(goToMainMenu:)];
            [self.background runAction: [CCSequence actions: actionInterval, actionCallFunc, nil]];
        }

        else if (CGRectContainsPoint([self.difficultyButton boundingBox], location)) {
            [self.volumeButton setTexture:[[CCTextureCache sharedTextureCache] addImage:@"VolumeButton.png"]];
        }
       }
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Add a new body/atlas sprite at the touched location
    for( UITouch *touch in touches ) {
        CGPoint location = [touch locationInView: [touch view]];
        NSLog(@"Touch");
        //missileButton* missileButton;
        location = [[CCDirector sharedDirector] convertToGL: location];

        if (CGRectContainsPoint([self.backButton boundingBox], location))
        {
            [self.backButton setTexture:[[CCTextureCache sharedTextureCache]addImage:@"BackButtonPressed.png"]];
            NSLog(@"BackButtonTouched");
        }

        else if (CGRectContainsPoint([self.volumeButton boundingBox], location))
            [self.volumeButton setTexture:[[CCTextureCache sharedTextureCache]addImage:@"VolumeButtonPressed.png"]];

    }
}



-(void) goToMainMenu:(id) sender
{
    [[CCDirector sharedDirector] replaceScene: [CCTransitionSlideInR transitionWithDuration:2 scene:[MainMenu node]]];
}
@end

I would appreciate any help in solving this issue.

2
Is backButton ever defined? In your init, shouldn't CCSprite *backButton = be _backButton =?Lorenzo Linarducci
Its a class property assigned in the heade like this: [code]@property (assign) CCSprite *backButton;[/code]user2970795
Correct, so that is never being set in this code. Instead, you are creating a SEPARATE backButton in the init. The self.backButton in the touch handler is not defined.Lorenzo Linarducci
you are right!, what a silly mistake to do. I'll fix that no and see if it runs correctly. Thank you!user2970795

2 Answers

0
votes

Your self.backButton is never defined. Change your init function to the following (I'm assuming all of your CCSprite defines are actually properties:

-(id) init
{

    if( (self=[super init]) ) {
        self.touchEnabled = YES;
        self.accelerometerEnabled = YES;


        CGSize winSize = [[CCDirector sharedDirector] winSize];

        _background = [CCSprite spriteWithFile:@"Menu0.png"];
        _background.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:_background z:0];

        _volumeBar = [CCSprite spriteWithFile:@"VolumeBar.png"];
        _volumeBar.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:_volumeBar z:1];

        _volumeButton = [CCSprite spriteWithFile:@"VolumeButton.png"];
        _volumeButton.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:_volumeButton z:1];

        _difficultyButton = [CCSprite spriteWithFile:@"EasyButton.png"];
        _difficultyButton.position = ccp(winSize.width/2, winSize.height/2-55);
        [self addChild:_difficultyButton z:1];

        _backButton = [CCSprite spriteWithFile:@"BackButton.png"];
        _backButton.position = ccp(winSize.width/12, winSize.height/8);
        [self addChild:_backButton z:1];

        [self scheduleUpdate];
    }

    return self;
}
0
votes

Another approach would be to use CCMenu and CCMenuItemSprite for all of your buttons. These classes make simple buttons for menus much easier to implement. Most/all of the touch handling is done for you and there are variants that use selectors or blocks for handling the button press.

This would save you a lot of code, and trouble.