Reputation: 11696
My SKSpriteNode is not appearing in the view when I am restoring from a saved state.
There are no errors or warnings during the save and restore process.
I am saving my player class using this code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_player];
[defaults setObject:data forKey:@"player"];
[defaults synchronize];
My player class has the following encoder/decoder:
-(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;
}
I then restore my player with this code:
NSData *savedPlayer = [[NSUserDefaults standardUserDefaults] objectForKey:@"player"];
_player = [NSKeyedUnarchiver unarchiveObjectWithData:savedPlayer];
The problem is that the player sprite is not visible after the restore. However, tapping on the screen, the player jumps (view moves up and down), swings his sword (sound effect is present) and fires missiles (missiles are visible and moving) - all 3 actions are initiated from the player class. I just cannot see the player sprite.
EDIT: Added video
Video of player class initialization.
Video of restore player class from saved state:
Upvotes: 0
Views: 340
Reputation: 1608
Sorry, this bit of an old question, but i'm just starting to setup saving my game as well and came across this. I didn't see any answers, so i thought i'd contribute.
I'm not sure if setting the texture like you are automatically sets the sprite size as well, I haven't tested this yet, but my sprites are originally being created with:
[SKSpriteNode spriteNodeWithTexture:<#Texture#> size:CGSizeMake(<#Width#>,<#Height#>)]
That method asks for both the texture and a size, and I noticed that you're re-setting the texture property, but missing a:
self.size = CGSizeMake(w,h)
in
-(instancetype)initWithCoder:(NSCoder *)decoder
Perhaps the sprites are being built, they just aren't visible because they have a width and height of 0?
As it turns out, self
just has to be initialized differently, and not with self = [super initWithCoder:decoder]
. Here's my complete init method on a playing card sprite:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithTexture:[aDecoder decodeObjectForKey:@"texture"] color:[UIColor clearColor] size:CGSizeMake(106,156)];
if(self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.theme = [aDecoder decodeObjectForKey:@"theme"];
self.suit = [aDecoder decodeObjectForKey:@"suit"];
self.value = [aDecoder decodeIntForKey:@"value"];
self.ID = [aDecoder decodeIntForKey:@"ID"];
self.locked = [aDecoder decodeBoolForKey:@"locked"];
self.faceUp = [aDecoder decodeBoolForKey:@"faceUp"];
self.black = [aDecoder decodeBoolForKey:@"black"];
self.dealCard = [aDecoder decodeBoolForKey:@"dealCard"];
self.highlighted = [aDecoder decodeBoolForKey:@"highlighted"];
self.highlightAction = [aDecoder decodeObjectForKey:@"highlightAction"];
self.inStackNode = [aDecoder decodeObjectForKey:@"inStackNode"];
self.shadowNode = [aDecoder decodeObjectForKey:@"shadowNode"];
self.highlightNode = [aDecoder decodeObjectForKey:@"highlightNode"];
self.zPosition = [aDecoder decodeFloatForKey:@"zPosition"];
[self addChild:self.shadowNode];
[self addChild:self.inStackNode];
}
return self;
}
Upvotes: 1