cocos2d子层访问父层的三种方法


情景设定:父层HelloWorldLayer有一个方法-(void) setlable;需要被其子层SecondLayer访问。

第一种、半单例方法:

首先在HelloWorldLayer.h声明+(HelloWorldLayer*) shareLayer

  1. +(HelloWorldLayer*) shareLayer;  

然后在HelloWorldLayer.m加入:

  1. #import "SecondLayer.h"  
  2.   
  3. static HelloWorldLayer* HelloWorldLayerInstance;  
  4.   
  5. +(HelloWorldLayer*) shareLayer  
  6. {  
  7.     return HelloWorldLayerInstance;  
  8. }  
  9.   
  10. -(id) init  
  11. {  
  12.     // always call "super" init  
  13.     // Apple recommends to re-assign "self" with the "super" return value  
  14.     if( (self=[super init])) {  
  15.         HelloWorldLayerInstance=self;  
  16.           
  17.         SecondLayer* sl=[[SecondLayer alloc] init];  
  18.         sl.tag=10;  
  19.         [self addChild:sl z:100];  
  20.         self.isTouchEnabled=YES;  
  21.           
  22.         ////.................  
  23.          
  24.     }  
  25.     return self;  
  26. }  
  27.   
  28. -(void) setlable  
  29. {  
  30.     clickNum++;  
  31.     [label setString:[NSString stringWithFormat:@"ParentLayer clickNum:%i",clickNum]];  
  32. }  

在SecondLayer就可以通过这样的方式来访问HelloWorldLayer的-(void) setlable方法:

  1. [[HelloWorldLayer shareLayer] setlable];  

第二种、self.parent强制访问方法:

HelloWorldLayer中只需按正常添加子层SecondLayer即可(HelloWorldLayer.m中):

  1. -(id) init  
  2. {  
  3.     // always call "super" init  
  4.     // Apple recommends to re-assign "self" with the "super" return value  
  5.     if( (self=[super initWithColor:ccc4(0, 255, 255,255)])) {  
  6.         HelloWorldLayerInstance=self;  
  7.           
  8.         SecondLayer* sl=[[SecondLayer alloc] init];  
  9.         sl.tag=10;  
  10.         [self addChild:sl z:100];  
  11.         self.isTouchEnabled=YES;  
  12.           
  13.         ////.................  
  14.          
  15.     }  
  16.     return self;  
  17. }  
在SecondLayer就可以通过这样的方式来访问HelloWorldLayer的-(void) setlable方法:
  1. [(HelloWorldLayer*)self.parent setlable];  
第三种、协议委托方法:

在SecondLayer.h中加入:

  1. #import <Foundation/Foundation.h>  
  2. #import "cocos2d.h"  
  3.   
  4. @protocol callParentDelegate <NSObject>  
  5. -(void) setlable;  
  6. @end  
  7.   
  8. @interface SecondLayer : CCLayer{  
  9.     id<callParentDelegate> delegate;  
  10. }  
  11. @property(nonatomic,retain) id<callParentDelegate> delegate;  
  12. @end  
SecondLayer.m中@synthesize delegate;

然后在HelloWorldLayer.h中加入<callParentDelegate>协议:

  1. @interface HelloWorldLayer :CCLayer <callParentDelegate>  
  2. {  
  3.     CCLabelTTF *label;  
  4.     int clickNum;  
  5. }  

在HelloWorldLayer.m中实现:

  1. -(void) setlable  
  2. {  
  3.     clickNum++;  
  4.     [label setString:[NSString stringWithFormat:@"ParentLayer clickNum:%i",clickNum]];  
  5. }  

在添加SecondLayer子层注意设子委托:

  1. SecondLayer* sl=[[SecondLayer alloc] init];  
  2. sl.tag=10;  
  3. sl.delegate=self;  
  4. [self addChild:sl z:100];  
  5. self.isTouchEnabled=YES;  
在SecondLayer就可以通过这样的方式来访问HelloWorldLayer的-(void) setlable方法:
  1. [self.delegate setlable];  

还有更好的办法,欢迎各位交流!

相关内容