在我的代码中,我在 中创建了一个实例ScrollingBackground
,BackgroundLayer
并且BackgroundLayer
还在GameScene
. 我想保存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 的正确方法吗?