Cocos2d:从另一个(父?)CCLayer中的一个CCLayer调用函数 [英] Cocos2d: Calling function from one CCLayer in another (parent?) CCLayer

查看:276
本文介绍了Cocos2d:从另一个(父?)CCLayer中的一个CCLayer调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello awesome dev community。
我已经找到了一个答案,几天了,只是不能找到一个句柄,所以决定自己发帖Q。



我有具有用于拾取物品的菜单的游戏。
菜单可以有2 - > 30+个项目,因此需要滚动。还有类别菜单,当一个项目被点击,一个新的菜单与该类别的项目出现。



我认为,最有效的方式去在专用图层上创建菜单所需的所有元素。
问题是,我不知道如何调用我的addNewItems:itemsArray函数定义在主游戏场景,从CCLayer包含菜单。



,我应该只使用一层吗?有点混乱,难以一起移动多个项目。



非常感谢您的任何帮助 - 或指向一个清晰的教程或如何做的示例因为我真的找不到任何。



下面是我要创建的流程图。



谢谢!



Hanaan



解决方案最终得到了David994A的回答(cocos2d forum)



答案是传递一个指向它父层的指针
我这样初始化CCLayer:



- (id)initWithParent:(CCLayer *)parentLayer;



并像这样调用:



ItemsMenuLayer * tempLayer = [[ItemsMenuLayer alloc] initWithParent:self];



然后,从CClayer里面我可以调用任何父层的函数,



[parentLayer functionName];


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

解决方案

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];

这篇关于Cocos2d:从另一个(父?)CCLayer中的一个CCLayer调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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