0

在我的场景中,我有

//。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

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

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

任何线索为什么会发生这种情况?我的场景写错了吗?

4

1 回答 1

2

在您调用 时[JoinedMapsLayer node],您尚未添加joinedMapsLayer为 的实例的子级JoinedMapsScene,因此它没有父级。

于 2012-04-14T22:55:06.697 回答