By dynamic I mean I don't know how many MenuItems I'm going to have. It depends on user input, because I looked at the code for the Menu class and it seems that once I've set a menu with items I can't change or alter that menu.
Any way to get a dynamic menu?
(13 posts) (3 voices)-
Posted 2 years ago #
-
I just use the regular addChild method e.g. [menu addChild:[MenuItemFont ...]];
And then run [menu alignItemsXXX]; after adding new items to reposition them correctly.
Posted 2 years ago # -
Does it work like that? I didn't know you could do that. Thanks!
Posted 2 years ago # -
Yea, I was pleasantly surprised by that too!
Posted 2 years ago # -
Wow, this is new, thanks for the info - useful!
Posted 2 years ago # -
Do we need to add the menuitems to the layer as well? Because I was reading that cocos doesn't recursively add all the children of a parent when the parent is added.
Posted 2 years ago # -
I'm not on my dev machine right now, but I believe I just added the menu to the layer as usual, and then added the fonts directly to the menu. I don't remember having to do any other additions although I can't be sure until I can check my code again (I added this a few weeks ago).
Are you having troubles getting it to work?
Posted 2 years ago # -
No I actually haven't tried it yet, I'm about to, if it doesn't work I'll just add each individual MenuItem as well.
Posted 2 years ago # -
So looking at the source for Menu, it seems that riq is simply using addChild as well in initwithitems, so this method should work!
Posted 2 years ago # -
For anyone that cares, here is very simple code to create a menu with an unknown amount of elements from an array.
Menu *menu = [Menu menuWithItems:nil];
[menu setPosition:ccp(0,0)];for (int i = 0; i<possibleAnswers.count;i++)
{MenuItem *item = [MenuItemFont itemFromString:[possibleAnswers objectAtIndex:i] target:self selector: @selector(MCAnswer:)];
item.transformAnchor = ccp (0,0);
[item setPosition:ccp (60, (i-1)*30 + 170)];
[menu addChild:item];Label *l = [Label labelWithString:[NSString stringWithFormat:@"%c.", c] fontName:@"Arial" fontSize:25];
[l setPosition:ccp (20, item.position.y)];
[self addChild:l];
c+=1;
}This is very useful for creating a quiz creator or something when you don't know how many MC possibilities you'll have. My variable c is simply a char that I increment and initialize at 'A'.
Hope this helps. Also, to check if the right one was pressed I'd do this (haven't actually written this part out yet but it works in my head!)
I'd have a MenuItem *correctAnswer, and as the programmer we should have some way of indicating which indice of the possibleAnswer array is the right answer and in the for loop when we hit that indice we simply set correctAnswer equal to that MenuItem we will be creating. When the click method is invoked (upon clicking an item on the menu), we simply check:
if (sender == correctAnswer)
{
[user sendMessage:@"YAAAY YOU GOT IT RIGHT!"];
}Hope it helps!
Posted 2 years ago # -
Glad you got it working, thanks for sharing your code for others ; )
I think I ended up extending the MenuItemFont class, and adding an extra data/value var to it, so I could check against it in the callback method via the sender, slightly different to your setup, but worked pretty well for me.
Posted 2 years ago # -
Yes that way works too I guess, I usually try to avoid data variables such as this as much as I can as it clusters up code and for me at least makes it harder to understand the code.
Posted 2 years ago # -
My usage is slightly different to yours. I needed each menu item to trigger a different action (saved in an array), so couldn't think of another way to handle that easily. If I had to just detect a single state, I'd of probably gone for your method too ; )
Posted 2 years ago #
Reply
You must log in to post.