如何在运行时为 Cocos2D CCMenu menuWithItems 正确动态创建 va_list?
我在 CCMenu 课程中度过了一段非常愉快的时光。要使用此类创建菜单,它会强制您调用名为 initWithItems 的方法,该方法采用 va_list。我需要在运行时生成这个列表,我读到创建一个 C 数组并传递它可以像 va_list 一样在幕后工作,只是它失败了。
我在 va_list 中有一个 NSArray,其中包含我想要的项目,这些项目是 CCMenuItem 的子类,CCMenuItem 是 menuWithItems 在 va_list 中期望的类。如果您在编译时硬编码此列表,它可以正常工作,但我动态创建此列表的尝试不起作用。这有什么问题吗? MenuItemButton 是 CCMenuItem 的子类。
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 应该以 null 终止,我不知道调用 getObjects 后我的代码是否属于这种情况,或者这是否是问题所在。
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.
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];
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地使用 nil 初始化菜单。例如,
然后假设您有一个在运行时加载的动态字符串列表,请尝试......
You could simply initialize the menu using nil. For example,
Then say you have a dynamic list of strings that you loaded at runtime, try....
va_list
并不总是数组。对于 32 位 gcc 来说是这样,对于 64 位则不然。不要依赖它。va_list
由获取可变数量参数的函数生成:va_list
isn't always an array. With 32-bit gcc, it is, with 64-bit it isn't. Don't rely on it.va_list
is generated by a function that gets a variable number of arguments: