Cocos2d:从一个CCLayer调用另一个(父级?)CCLayer中的函数
你好,很棒的开发社区。 我已经寻找这个问题的答案好几天了,但似乎找不到句柄,所以决定自己发布 Q。
我有一个游戏,有用于挑选物品的菜单。 菜单可以有 2 -> 30 多个项目,因此需要滚动。还有类别菜单,当单击某个项目时,会出现一个包含该类别项目的新菜单。
我认为最有效的方法是在专用层上创建菜单所需的所有元素。 问题是我不知道如何从包含菜单的 CCLayer 调用主游戏场景中定义的 addNewItems:itemsArray 函数。
或者,我应该只使用一层吗?有点混乱并且难以将多个物品移动到一起。
非常感谢您的任何帮助 - 或者为我指明了清晰的教程或如何执行此操作的示例的方向,因为老实说我找不到任何帮助。
下面是我想要创建的流程图。
谢谢!!
哈纳
Hello awesome dev community.
I have looked for an answer to this for days now and just can't seem to find a handle, so decided to post Q myself.
I have a game that has menus for picking items.
The menus can have 2 -> 30+ items, so they need to scroll. There are also category menus, which when an item is clicked, a new menu with that category's items appears.
I figured that the most efficient way to go about it is by creating all the elements needed for the menu on a dedicated layer.
The issue is that I have no idea how to call my addNewItems:itemsArray function defined in the main game scene, from the CCLayer containing the menu.
Or, should I just use one layer? A bit messy and difficult to move multiple items together.
Thank you so much for any help - or pointing me in the direction of a clear tutorial or examples of how to do that since I honestly couldn't find any.
Below is a flowchart of what I'm looking to create.
Thanks!!
Hanaan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于从David994A(cocos2d论坛)得到了答案
答案是向层传递一个指向其父层的指针
我像这样初始化 CCLayer:
-(id) initWithParent:(CCLayer *)parentLayer;
并像这样调用它:
ItemsMenuLayer *tempLayer = [[ItemsMenuLayer alloc] initWithParent:self];
然后,从 CClayer 内部我可以调用任何父层函数,如下所示:
[parentLayer functionName];
Finally got an answer from David994A (cocos2d forum)
The answer is to pass the layer a pointer to it's parent layer
I initialize the CCLayer like this:
-(id) initWithParent:(CCLayer *)parentLayer;
and call it like this:
ItemsMenuLayer *tempLayer = [[ItemsMenuLayer alloc] initWithParent:self];
Then, from inside the CClayer I can call any of the parent's layer functions, like this:
[parentLayer functionName];