mattblessed
mattblessed

Reputation: 801

Cocos2d Cant select menu items after moving them

Im trying to create the illusion of a drop down menu, but after i move the sub menu items i cant select them any more?

here my entire code:

#import "HelloWorldLayer.h"


CCMenuItem *playDown;
CCMenuItem *playUp;
CCMenuItemToggle *play;
CCMenuItem *help;
CCMenuItem *options;
int down;

// HelloWorld implementation
@implementation HelloWorldLayer

+(id) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

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

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

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init {
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) 
    {
        self.isTouchEnabled = TRUE;

        [CCMenuItemFont setFontSize:70];
        playDown = [CCMenuItemFont itemFromString:@"Play" target:self selector:@selector(playDown:)];
        playUp = [CCMenuItemFont itemFromString:@"Play" target:self selector:@selector(playUp:)];
        play = [CCMenuItemToggle itemWithTarget:self selector:@selector(playDown:) items:playDown,playUp, nil];

        help = [[CCMenuItemFont itemFromString:@"Help" target:self selector:@selector(help:)] retain]; 
        help.position = ccp(512,350);

        CCMenu *menu = [CCMenu menuWithItems:play,help, nil];
        [self addChild:menu];
        play.position = ccp(0,300);

        down = 0;

        [self schedule:@selector(itemSelected) interval:0.01];

    }
    return self;
}
    -(void) playDown: (id) sender {
        if (down == 0) {
            if ([help parent] != self) {
                help = [[CCMenuItemFont itemFromString:@"Help" target:self selector:@selector(help:)] retain];
                [self addChild:help];
                help.position = ccp(512,350);
                [help runAction:[CCMoveTo actionWithDuration:1 position:ccp(512,500)]];
                NSLog(@"Added Help");
            }
            if ([options parent] != self) {
                options = [[CCMenuItemFont itemFromString:@"Options" target:self selector:@selector(options:)] retain];
                [self addChild:options];
                options.position = ccp(512,650);
                [options runAction:[CCMoveTo actionWithDuration:1 position:ccp(512,600)]];
                NSLog(@"Added Options");
                down = 1;
            }
            return;
        }

        if (down == 1) {
            if ([options parent] == self) {
                [self removeChild:options cleanup:YES];
            }
            if ([help parent] == self) {
                [self removeChild:help cleanup:YES];
                down = 0;
            }
            return;
        }
    }

    -(void) playUp: (id) sender {

    }

    -(void) help: (id) sender {
        NSLog(@"Help Selected");
    }
    -(void) options: (id) sender {
        NSLog(@"Options Seleted");
    }

    -(void) itemSelected {
        if (help.isSelected) {
            [self runAction:[CCCallFunc actionWithTarget:self selector:@selector(help:)]];
        }
        if (options.isSelected) {
            [self runAction:[CCCallFunc actionWithTarget:self selector:@selector(options:)]];
        }
    }

- (void) dealloc
{

    [super dealloc];
}
@end

everything else seems to be working like adding the sprites and moving the menu items, I use a toggle to signal weather to send the drop down menu menu to show to not to show

Upvotes: 0

Views: 672

Answers (1)

YvesLeBorg
YvesLeBorg

Reputation: 9089

CCMenuItem is meant to be child of a CCMenu (not CCScene), and offers no touch processing per se. Thus, you need to create one of more CCMenu's to which you will add one or more CCMenuItem's. It is the CCMenu object that will process the touch handling, and invoke the menuItems as appropriate, depending on which menuItem the touch(es) occur, and the current state of the menuItem at the time of the touch event.

I also see some memory leak potentials in your code. When you addChild to any coco object, the object you are adding is retained, thus you need not retain it (in most circumstances). When the parent's cleanup method is invoked, the retained objects are released. Since they are autorelease'd objects, they will eventually be deallocated without your need for further attention. If you want to retain them, make certain you release these retained objects in your dealloc (or cleanup) method.

Upvotes: 1

Related Questions