Cocos2d iphone 使用Cocos2d中的对象初始化CCSprite

Cocos2d iphone 使用Cocos2d中的对象初始化CCSprite,cocos2d-iphone,Cocos2d Iphone,我正在尝试将分数从我的主层传递到我的一个CCSprite,该CCSprite根据所述分数进行初始化。当我使用以下命令初始化CCSprite时: //Main Layer Platform *platform = [[Platform alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"platform.png"] withScore:score]; //CCSpri

我正在尝试将分数从我的主层传递到我的一个CCSprite,该CCSprite根据所述分数进行初始化。当我使用以下命令初始化CCSprite时:

//Main Layer
Platform *platform = [[Platform alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"platform.png"] withScore:score];

//CCSprite Layer
-(id) initWithSpriteFrame:(CCSpriteFrame*)spriteFrame withScore:(int)initScore {
    if( (self=[super init]) ) {
    ...
    }
}
//Main Layer
Platform *platform = [[Platform alloc]initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"platform.png"]];
 [platform setScore:score];

//CCSprite Layer
@property (readwrite) int score;
@synthesize score;

-(id) init {
    if( (self=[super init]) ) {
    ...
    }
}
我得到了一个分数,没有雪碧纹理

随后,当我使用以下命令初始化时:

//Main Layer
Platform *platform = [[Platform alloc] initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"platform.png"] withScore:score];

//CCSprite Layer
-(id) initWithSpriteFrame:(CCSpriteFrame*)spriteFrame withScore:(int)initScore {
    if( (self=[super init]) ) {
    ...
    }
}
//Main Layer
Platform *platform = [[Platform alloc]initWithSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"platform.png"]];
 [platform setScore:score];

//CCSprite Layer
@property (readwrite) int score;
@synthesize score;

-(id) init {
    if( (self=[super init]) ) {
    ...
    }
}

我得到了一个没有分数的CCSprite纹理(它是0)。男人该怎么做?

您需要将精灵框架传递给super,因此在您的第一个代码示例中,将其更改为此

//CCSprite Layer
-(id) initWithSpriteFrame:(CCSpriteFrame*)spriteFrame withScore:(int)initScore {
    if( (self=[super initWithSpriteFrame:spriteFrame]) ) {
      ...
    }

    return self; //Make sure to return self!
}