我对 a 进行了子类化CCSprite
以制作一个可以编码和解码的对象。我想保存精灵状态并在特定位置再次加载它。NSKeyedUnarchiver
除了用(见loadIconSpriteState
下文)解码之外,一切似乎都很好,EXC_BAD_ACCESS
下面是代码:
HelloWorldLayer.h
@interface CCIconSprite : CCSprite {
NSString *iconName;
float iconXPos;
float iconYPos;
}
@property (nonatomic, retain) NSString *iconName;
@property (nonatomic, assign) float iconXPos;
@property (nonatomic, assign) float iconYPos;
+ (id)iconWithType:(NSString*)imageName;
- (id)initWithIconType:(NSString*)imageName;
@end
@interface HelloWorldLayer : CCLayer < NSCoding>
{
CCIconSprite* testSprite;
BOOL savedState;
CGSize size;
CCMoveTo* moveTo;
NSMutableArray* saveSpriteArray;
NSData* savedSpriteData;
}
+(CCScene *) scene;
@end
HelloWorldLayer.m:
CCIconSprite 实现:
@implementation CCIconSprite
@synthesize iconXPos;
@synthesize iconYPos;
@synthesize iconName;
+ (id)iconWithType:(NSString*)imageName
{
return [[[[self class] alloc] initWithIconType:imageName] autorelease];
}
- (id)initWithIconType:(NSString*)imageName
{
self = [super initWithFile:imageName];
if (!self) return nil;
iconName = imageName;
self.position = ccp(iconXPos, iconYPos);
return self;
}
编码和解码:
- (id)initWithCoder:(NSCoder *)decoder {
NSString* imageFileName = [decoder decodeObjectForKey:@"imageFileName"];
self = [self initWithIconType:imageFileName];
if (!self) return nil;
self.iconXPos = [decoder decodeFloatForKey:@"iconXPos"];
self.iconYPos = [decoder decodeFloatForKey:@"iconYPos"];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:iconName forKey:@"imageFileName"];
[encoder encodeFloat:self.iconXPos forKey:@"iconXPos"];
[encoder encodeFloat:self.iconYPos forKey:@"iconYPos"];
}
@end
HelloWorldLayer 实现:
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init]) ) {
self.scale = 0.5;
savedState = NO;
size = [[CCDirector sharedDirector] winSize];
testSprite = [CCIconSprite spriteWithFile:@"Icon.png"];
testSprite.position = ccp(size.width/4, size.width/4);
testSprite.anchorPoint = ccp(0,0);
[self addChild:testSprite];
moveTo = [CCMoveTo actionWithDuration:3 position:ccp(3*size.width/4, 3*size.width/4)];
[testSprite runAction:moveTo];
[self schedule:@selector(saveAndLoadSpriteState)];
}
return self;
}
保存和加载状态:
-(void)saveIconSpriteState {
saveSpriteArray = [[NSMutableArray alloc] init];
[saveSpriteArray addObject:testSprite];
savedSpriteData = [NSKeyedArchiver archivedDataWithRootObject:saveSpriteArray];
}
-(void)loadIconSpriteState {
[NSKeyedUnarchiver unarchiveObjectWithData:savedSpriteData];
}
-(void)saveAndLoadSpriteState {
if ((testSprite.position.x > size.width/2) && !savedState) {
savedState = YES;
[self saveIconSpriteState];
}
else if ((testSprite.position.x == 3*size.width/4) && savedState) {
savedState = NO;
[self loadIconSpriteState];
[testSprite runAction:moveTo];
}
}
@end
编辑
设置异常断点后出现以下错误
Assertion failure in -[CCIconSprite initWithFile:]
指向行内:
NSAssert(filename != nil, @"Invalid filename for sprite");
在里面
-(id) initWithFile:(NSString*)filename
CCSprite.m 类的方法。