在我的代码中,我在ScrollingBackground
中创建了BackgroundLayer
的实例,而BackgroundLayer
也在GameScene
中创建了一个实例。我想保存ScrollingBackground
状态,所以当玩家死亡时我可以保持不变。以下是相关代码:
ScrollingBackground.m:
-(id)initWithBackgroundLayer: (BackgroundLayer *)layer{
if (self = [super init]) {
backgroundRepeatCount =0;
scrollSpeed = 0.0;
backgroundCount = 0;
backgroundArray = [[NSMutableArray alloc] init]
bg = [CCSprite spriteWithSpriteFrameName:@"bg1.png"];
bg.position = ccp(s.width/2, s.height/2);
[currentBatchNode addChild:bg];
bgSwap = [CCSprite spriteWithSpriteFrameName:@"bg2.png"];
bgSwap.position = ccp(s.width/2, 3*s.height/2 -1.0);
[currentBatchNode addChild: bgSwap];
.
.
.
}
}
-(void) encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.bg forKey:@"bg"];
[aCoder encodeObject:self.bgSwap forKey:@"bgSwap"];
[aCoder encodeObject:self.displayedBg forKey:@"displayedBg"];
[aCoder encodeObject:self.backgroundArray forKey:@"backgroundArray"];
[aCoder encodeObject:self.currentBatchNode forKey:@"currentBatchNode"];
.
.
.
}
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
self.bg = [aDecoder decodeObjectForKey:@"bg"];
self.bgSwap = [aDecoder decodeObjectForKey:@"bgSwap"];
self.displayedBg = [aDecoder decodeObjectForKey:@"displayedBg"];
self.backgroundArray = [aDecoder decodeObjectForKey:@"backgroundArray"];
self.currentBatchNode = [aDecoder decodeObjectForKey:@"currentBatchNode"];
.
.
.
}
}
编辑:
如何保存上述状态?
如何重新加载?
这是保存CCSprites和CCSpriteBatchNodes的正确方法吗?