el.severo
el.severo

Reputation: 2277

cocos2d creating dynamic menu lists

How do I create a CCMenuItem list dynamically?

//Returns me an array with my items
Items *items = [ItemParser loadItemsForLevel:selectedLevel fromSuperLevel:selectedSuperLevel];

For an item I have a string with the name of the item that I'd like to display in my CCMenu. The number of items could vary but I want to display only 6 items at a time

and how do I remove it? I'm cleaning up from the CCLayer but I'd like to do it also from the menu list

Anyone?

Upvotes: 0

Views: 617

Answers (1)

phlebotinum
phlebotinum

Reputation: 1130

Cocos2D does not provide a method to do this.

You may create your own initializer based on the original one found in "CCMenu.m". The original looks like this (I removed code that does not add items here for clarity). Create your own init method based on the original and add a variable amount of items instead. If you like you may also set it up as a category to CCMenu.

-(id) initWithItems: (CCMenuItem*) item vaList: (va_list) args
{
    if( (self=[super init]) ) {

        // ... code cut for clarity

        if (item) {
            [self addChild: item z:z];
            CCMenuItem *i = va_arg(args, CCMenuItem*);
            while(i) {
                z++;
                [self addChild: i z:z];
                i = va_arg(args, CCMenuItem*);
            }
        }

        // ... code cut for clarity

    }

    return self;
}

Update: When your menu items change then rebuild the whole menu.

Upvotes: 1

Related Questions