Cocos2D:无法在父类中调用方法

时间:2012-04-13 19:27:18

标签: methods cocos2d-iphone call parent scene

在我的场景中我有

//的.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

//。米

#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

在joinedMapsLayer init方法中,我尝试调用getInt并返回它的值100,但它返回0:

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

为什么会发生这种情况的任何线索?我的场景写得不正确吗?

1 个答案:

答案 0 :(得分:2)

当您致电[JoinedMapsLayer node]时,您尚未将joinedMapsLayer添加为JoinedMapsScene实例的子级,因此它没有父级。