Cocoa touch 如何在NSUserDefault中存储CCSprite

Cocoa touch 如何在NSUserDefault中存储CCSprite,cocoa-touch,cocos2d-iphone,Cocoa Touch,Cocos2d Iphone,我在使用CCSprite子类生物时遇到了一个奇怪的问题 让我们,我的对象是生物 类生物声明- @interface Creature : CCSprite <NSCoding>{ int creatureAge; NSString *creatureName; } 问题是当我使用NSUserDefault存储我的生物类对象“生物”时- - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encode

我在使用CCSprite子类生物时遇到了一个奇怪的问题

让我们,我的对象是生物

类生物声明-

@interface Creature : CCSprite <NSCoding>{

    int creatureAge;
    NSString *creatureName;
}
问题是当我使用NSUserDefault存储我的生物类对象“生物”时-

- (void)encodeWithCoder:(NSCoder *)encoder {

    [encoder encodeObject:self.creatureName forKey:@"creatureName"];
    [encoder encodeInt:self.creatureAge forKey:@"creatureAge"];
}
然后用计算机解码-

- (id)initWithCoder:(NSCoder *)decoder {
    if((self = [super init])) {

        self.creatureName = [decoder decodeObjectForKey:@"creatureName"];
        self.creatureAge= [decoder decodeIntForKey:@"creatureAge"];
}
然后使用-

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:creature];

[defaults setObject:myEncodedObject forKey:@"my_pet"];
而负载—

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSData *myEncodedObject = [defaults objectForKey:@"my_pet"];
 Creature* newcreature = (Creature *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
问题是,当我加载这个时,我得到了以前存储的生物的属性值,但分配给以前生物的图像可能没有复制。因为如果我将新生物添加到任何CCLayer中,它不会显示任何图像,尽管它获得了前一个生物的属性值


现在我该怎么做才能得到有图像的新生物?是否需要将图像名称作为一个单独的属性添加?

您也可以简单地存储类型,然后按如下方式执行:

@interface Creature : CCSprite {
    int creatureType;
    int creatureAge;
    NSString *creatureName;
}

+ (id)creatureWithType:(int)type;
- (id)initWithCreatureType:(int)type;
@end

@implementation Creature

+ (id)creatureWithType:(int)type
{
    return [[[[self class] alloc] initWithCreatureType:type] autorelease];
}

- (id)initWithCreatureType:(int)type
{
    self = [super initWithFile:[NSString stringWithFormat:@"ch%i_default.png", type]];
    if (!self) return nil;

    creatureType = type;

    return self;
}

- (id)initWithCoder:(NSCoder *)decoder {
    int type = [decoder decodeIntForKey:@"creatureType"];
    self = [self initWithCreatureType:type];
    if (!self) return nil;

    self.creatureName = [decoder decodeObjectForKey:@"creatureName"];
    self.creatureAge= [decoder decodeIntForKey:@"creatureAge"];

    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt:creatureType forKey:@"creatureType"];
    [encoder encodeObject:self.creatureName forKey:@"creatureName"];
    [encoder encodeInt:self.creatureAge forKey:@"creatureAge"];
}
@end

您可能还希望通过属性公开
creatureType
。请注意,与其使用
初始化creatureWithType:
名称,不如使用
creatureWithType:

名称“更多Cocoa”,谢谢您的回复。我知道,但类型是不够的,因为它可能需要ch%I\u默认值、ch%I\u悲伤、ch%I\u快乐等,所以需要NSString。然而,这并不是主要的问题,我不清楚是否需要将图像编码为单独的属性。事实上,起初我认为图像将被包装在对象本身中,所以不需要明确地添加它。有人能解释为什么不会发生吗???您可以将生成的字符串(如
ch1_default.png
)存储在一个实例变量中,并按照我用int显示的方式对其进行编码/解码,然后在
initWithCoder:
中调用
self=[self initWithFile:decodedString]。如果要存储图像,请使用将
UIImage
转换为
NSData
,以及
[UIImage imageWithData://code>。好的,答案很可能是-需要对属于特定类的所有内容进行编码/解码。所以,如果我想要与对象对应的图像,那么也需要对其进行编码/解码。
@interface Creature : CCSprite {
    int creatureType;
    int creatureAge;
    NSString *creatureName;
}

+ (id)creatureWithType:(int)type;
- (id)initWithCreatureType:(int)type;
@end

@implementation Creature

+ (id)creatureWithType:(int)type
{
    return [[[[self class] alloc] initWithCreatureType:type] autorelease];
}

- (id)initWithCreatureType:(int)type
{
    self = [super initWithFile:[NSString stringWithFormat:@"ch%i_default.png", type]];
    if (!self) return nil;

    creatureType = type;

    return self;
}

- (id)initWithCoder:(NSCoder *)decoder {
    int type = [decoder decodeIntForKey:@"creatureType"];
    self = [self initWithCreatureType:type];
    if (!self) return nil;

    self.creatureName = [decoder decodeObjectForKey:@"creatureName"];
    self.creatureAge= [decoder decodeIntForKey:@"creatureAge"];

    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInt:creatureType forKey:@"creatureType"];
    [encoder encodeObject:self.creatureName forKey:@"creatureName"];
    [encoder encodeInt:self.creatureAge forKey:@"creatureAge"];
}
@end