你如何正确地在运行时的Cocos2D CCMenu menuWithItems动态创建的va_list? [英] How do you properly create a va_list dynamically at runtime for Cocos2D CCMenu menuWithItems?

查看:241
本文介绍了你如何正确地在运行时的Cocos2D CCMenu menuWithItems动态创建的va_list?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与CCMenu上课。地狱。要创建一个菜单,这个类它迫使你调用一个名为initWithItems方法,它的va_list。我需要生成这个名单在运行时,我读了创建C数组和传球为va_list的不被窝里能仅仅发挥作用,只有它是失败的。

I'm having a hell of a time with the CCMenu class. To create a menu with this class it forces you to call a method called initWithItems, which takes a va_list. I need to generate this list at runtime, and I read that creating a C array and passing that can function just as va_list does under the covers, only it is failing.

我在va_list的欲物品一个NSArray,这些项目是CCMenuItem的一个子类,即menuWithItems在va_list的期望的类。如果你硬code这个名单在编译的时候,它工作正常,但我试图动态创建这个列表是行不通的。有什么不对呢? MenuItemButton是CCMenuItem子类。

I have an NSArray of items I want in the va_list, and these items are a SUBCLASS of CCMenuItem, the class that menuWithItems is expecting in the va_list. If you hardcode this list at compile time, it works fine, but my attempt to create this list dynamically is not working. What is wrong with this? MenuItemButton is a CCMenuItem subclass.

NSArray *menuItems = [self getMenuItemsArray]; // Returns an NSArray of MenuItemButtons 
MenuItemButton *argList = (MenuItemButton *)malloc( sizeof(MenuItemButton *) * [menuItems count] );
[menuItems getObjects:(id *)argList];
CCMenuAdvanced* menu = [CCMenuAdvanced menuWithItems:argList];

这崩溃在运行时,BAD_ACCESS。我知道va_list的应该是空终止的,我不知道这是否是调用getObjects后我的code这里的情况,或者,甚至是这个问题。

This crashes at runtime, BAD_ACCESS. I know the va_list is supposed to be null terminated, I don't know if that is the case with my code here after calling getObjects, or if that is even the problem.

推荐答案

您可以简单地使用零初始化菜单。例如,

You could simply initialize the menu using nil. For example,

CCMenu * myMenu = [CCMenuAdvanced menuWithItems:nil];

然后说你有,你在运行时加载的字符串的动态列表,尝试....

Then say you have a dynamic list of strings that you loaded at runtime, try....

// replace this with a dynamically loaded array of items...
NSArray* dynamicArray = [NSArray arrayWithObjects:@"red", @"blue", @"green", nil];


for (NSString* item in dynamicArray)
{
    CCMenuItem *menuItem = [CCMenuItemFont itemFromString: item target: self     selector:@selector(menuCallback:)];
    [myMenu addChild:menuItem];
}

这篇关于你如何正确地在运行时的Cocos2D CCMenu menuWithItems动态创建的va_list?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆