Question

In my scene I have

//.h

#import "cocos2d.h"

#import "FixedBackground.h"
@class FixedBackground;

#import "JoinedMapsLayer.h"
@class JoinedMapsLayer;


@interface JoinedMapsScene : CCScene {


    FixedBackground *fixedBackground;
    JoinedMapsLayer *joinedMapsLayer;

}

@property(nonatomic, retain) FixedBackground *fixedBackground;
@property(nonatomic, retain) CCNode *joinedMapsLayer;

+(id) scene;

- (void) moveBG:(float)x andY:(float)y;
- (int) getInt;


@end

//.m

#import "JoinedMapsScene.h"

@implementation JoinedMapsScene

@synthesize fixedBackground;
@synthesize joinedMapsLayer;

+(id) scene {

    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layers' are an autorelease object.
    JoinedMapsScene *layer1 = [JoinedMapsScene node];

    // add layers as a childs to scene
    [scene addChild: layer1];

    return scene;
}

-(id) init {

    if( (self=[super init] )) {

        fixedBackground = [FixedBackground node];
        joinedMapsLayer  = [JoinedMapsLayer node];

        // add layers as a children of the scene
        [self addChild:fixedBackground];
        [self addChild:joinedMapsLayer];

    }
    return self;
}

- (int)getInt {
    return 100;
}

- (void) dealloc{

    [super dealloc];
}

@end

In joinedMapsLayer init method I attempt to call getInt and return it's value of 100, but it returns 0:

NSLog(@"%d",[(JoinedMapsScene*)self.parent getInt]);

Any clue to why this is happening? Do I have my scene written incorrectly?

Was it helpful?

Solution

At the time you call [JoinedMapsLayer node], you have not yet added joinedMapsLayer as a child of the instance of JoinedMapsScene, so it has no parent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top