0

我对 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 类的方法。

4

1 回答 1

0

只是一种预感,但也许就是这样。解码字符串时,您应该复制它,因为此时您不拥有它,也没有将其分配给 strong 或 copy 属性。

- (id)initWithCoder:(NSCoder *)decoder {
   NSString* imageFileName = [[decoder decodeObjectForKey:@"imageFileName"] copy];
   self = [self initWithIconType:imageFileName];
   if (!self) return nil;
   ...
}

如果不是这样,请设置断点并验证字符串在编码和解码时确实正确。

于 2013-11-14T00:20:48.573 回答