【问题标题】:Cocos2D: Cannot call method in parent classCocos2D:无法调用父类中的方法
【发布时间】:2012-04-13 19:27:17
【问题描述】:

在我的场景中

//.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]);

有什么线索可以解释为什么会这样吗?我的场景写错了吗?

【问题讨论】:

  • 你也可以写.h文件吗?我认为问题在于结构化......

标签: methods cocos2d-iphone call parent scene


【解决方案1】:

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

【讨论】:

  • 谢谢 我现在明白了。我会想办法做到这一点。
猜你喜欢
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 2011-01-03
相关资源
最近更新 更多