user2766367
user2766367

Reputation: 399

Not accessing function in Objective-C

I'm currently trying to access a function on another file.(another layer, to be more precise).

Both layers are on a scene.

Third layer is trying to get a function from first layer...

Here's how I'm doing this:

Here's my scene in scene.h

 #import "firstLayer.h"
 #import "secondLayer.h"
 #import "thirdLayer.h"

@interface myScene : CCScene
{
// custom instance vars here...
}
@end

Here's how I cast my scene in scene.m

-(id)init {
self = [super init];
if(self != nil){

firstLayer *firstLayerz = [firstLayer node];
    [firstLayerz setTag:111];
    [self addChild:firstLayerz z:0];


    secondLayer *secondLayerz = [secondLayer node];
    [secondLayer setTag:112];
    [self addChild:secondLayer z:2];


    thirdLayer *thirdLayerz = [thirdLayer node];
    [thirdLayerz setTag:113];
    [self addChild:thirdLayerz z:4];

Here's how I cast the function in thirdLayer.m

#import "scene.h"

@implementation thirdLayer.m

-(id)init {
self = [super init];
if(self != nil){

     firstLayer* firstLayerz = (firstLayer*)[self.parent getChildByTag:111];

     [firstLayerz functionNeeded];

}

Here's functionNeeded in firstLayer.m (right below init(

    -(void)functionNeeded {
NSLog(@"inside fnnction needed");
}

Of course the log ain't showing...

I do the proper cast in firstLayer.h

 @interface firstLayer : CCLayer {

 }


-(void)functionNeeded;


@end

Upvotes: 1

Views: 94

Answers (1)

Renaissance
Renaissance

Reputation: 554

In your firstLayer's init method write

self.tag = 111;

Now in your ThirdLayer where you want to call method of first layer write :

CCScene *current = [[CCDirector sharedDirector] runningScene];
if (current) {
   id layer = [current getChildByTag:111];
   if (layer) {
      [layer functionNeeded];
   }
}

Upvotes: 1

Related Questions