当我从保存状态恢复时,我的 SKSpriteNode 没有出现在视图中。
在保存和恢复过程中没有错误或警告。
我正在使用以下代码保存我的播放器类:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_player];
[defaults setObject:data forKey:@"player"];
[defaults synchronize];
我的播放器类具有以下编码器/解码器:
-(void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.currentPlatform forKey:@"currentPlatform"];
[encoder encodeObject:[NSNumber numberWithBool:self.floorContact] forKey:@"floorContact"];
[encoder encodeObject:[NSNumber numberWithInt:self.currentState] forKey:@"currentState"];
// and many more...
}
-(instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
_animations = [Animations sharedInstance];
self.texture = [_animations playerStart];
self.texture.filteringMode = SKTextureFilteringNearest;
self.name = @"player";
self.zPosition = 999;
// more physicsBody stuff...
self.currentPlatform = [decoder decodeObjectForKey:@"currentPlatform"];
self.floorContact = [[decoder decodeObjectForKey:@"floorContact"] boolValue];
self.currentState = [[decoder decodeObjectForKey:@"currentState"] intValue];
// and many more...
}
return self;
}
然后我使用以下代码恢复我的播放器:
NSData *savedPlayer = [[NSUserDefaults standardUserDefaults] objectForKey:@"player"];
_player = [NSKeyedUnarchiver unarchiveObjectWithData:savedPlayer];
问题是播放器精灵在恢复后不可见。然而,点击屏幕,玩家跳跃(视图上下移动)、挥剑(存在音效)和发射导弹(导弹可见且移动)——所有 3 个动作都是从玩家类启动的。我只是看不到玩家精灵。
编辑:添加视频
从保存状态恢复播放器类的视频: