cocos2d访问父层的三种方法
来源:互联网 发布:公司前台 知乎 编辑:程序博客网 时间:2024/05/18 01:05
情景设定:父层HelloWorldLayer有一个方法-(void) setlable;需要被其子层SecondLayer访问。
第一种、半单例方法:
首先在HelloWorldLayer.h声明+(HelloWorldLayer*) shareLayer
- +(HelloWorldLayer*) shareLayer;
然后在HelloWorldLayer.m加入:
- #import "SecondLayer.h"
- static HelloWorldLayer* HelloWorldLayerInstance;
- +(HelloWorldLayer*) shareLayer
- {
- return HelloWorldLayerInstance;
- }
- -(id) init
- {
- // always call "super" init
- // Apple recommends to re-assign "self" with the "super" return value
- if( (self=[super init])) {
- HelloWorldLayerInstance=self;
- SecondLayer* sl=[[SecondLayer alloc] init];
- sl.tag=10;
- [self addChild:sl z:100];
- self.isTouchEnabled=YES;
- ////.................
- }
- return self;
- }
- -(void) setlable
- {
- clickNum++;
- [label setString:[NSString stringWithFormat:@"ParentLayer clickNum:%i",clickNum]];
- }
在SecondLayer就可以通过这样的方式来访问HelloWorldLayer的-(void) setlable方法:
- [[HelloWorldLayer shareLayer] setlable];
第二种、self.parent强制访问方法:
HelloWorldLayer中只需按正常添加子层SecondLayer即可(HelloWorldLayer.m中):
- -(id) init
- {
- // always call "super" init
- // Apple recommends to re-assign "self" with the "super" return value
- if( (self=[super initWithColor:ccc4(0, 255, 255,255)])) {
- HelloWorldLayerInstance=self;
- SecondLayer* sl=[[SecondLayer alloc] init];
- sl.tag=10;
- [self addChild:sl z:100];
- self.isTouchEnabled=YES;
- ////.................
- }
- return self;
- }
在SecondLayer就可以通过这样的方式来访问HelloWorldLayer的-(void) setlable方法:
- [(HelloWorldLayer*)self.parent setlable];
第三种、协议委托方法:
在SecondLayer.h中加入:
- #import <Foundation/Foundation.h>
- #import "cocos2d.h"
- @protocol callParentDelegate <NSObject>
- -(void) setlable;
- @end
- @interface SecondLayer : CCLayer{
- id<callParentDelegate> delegate;
- }
- @property(nonatomic,retain) id<callParentDelegate> delegate;
- @end
然后在HelloWorldLayer.h中加入<callParentDelegate>协议:
- @interface HelloWorldLayer :CCLayer <callParentDelegate>
- {
- CCLabelTTF *label;
- int clickNum;
- }
在HelloWorldLayer.m中实现:
- -(void) setlable
- {
- clickNum++;
- [label setString:[NSString stringWithFormat:@"ParentLayer clickNum:%i",clickNum]];
- }
在添加SecondLayer子层注意设子委托:
- SecondLayer* sl=[[SecondLayer alloc] init];
- sl.tag=10;
- sl.delegate=self;
- [self addChild:sl z:100];
- self.isTouchEnabled=YES;
在SecondLayer就可以通过这样的方式来访问HelloWorldLayer的-(void) setlable方法:
- [self.delegate setlable];
- cocos2d子层访问父层的三种方法
- cocos2d访问父层的三种方法
- Spring+Hibernate DAO 持久层开发, Spring 用 Hibernate 访问数据库的三种方法.推荐使用回调
- Spring+Hibernate DAO 持久层开发, Spring 用 Hibernate 访问数据库的三种方法.推荐使用回调
- 结构成员访问的三种方法
- python字典访问的三种方法
- python字典访问的三种方法
- python 字典访问的三种方法
- python 字典访问的三种方法
- opencv访问像素的三种方法
- 访问数组元素的三种方法
- python 字典访问的三种方法
- python 字典访问的三种方法
- python 字典访问的三种方法
- dwr访问service层的方法
- Cocos2d中添加手势支持的三种方法
- Cocos2d中添加手势支持的三种方法
- 解决Cocos2d-x中文乱码的三种方法
- 对于c语言的关键字
- Struts2标签
- XCode打包项目到Framework包
- 纯html标签下单引号和双引号以及html和JS混编下单引号和双引号,php引号的使用
- ZMQ
- cocos2d访问父层的三种方法
- 在Linux下获取帮助
- 用java.lang.Character把一个String数字小写字母变成大写
- linux命令学习(1)
- 【转】SurfaceFlinger GraphicBuffer内存共享缓冲区机制
- 统计crontab中定时任务耗时脚本
- 程序人生[流程图]
- Ubunto 防火墙
- Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(五)