2012-01-21 25 views
1

使用CCSprite子類Creature時出現奇怪的問題。如何在NSUserDefault中存儲CCSprite

讓我們的目標是生物*生物;

類生物申述

@interface Creature : CCSprite <NSCoding>{ 

    int creatureAge; 
    NSString *creatureName; 
} 

實施

+(id)initializeCreatureWithType:(int)type 
{ 

    Creature *creature = nil; 




    creature = [[[super alloc] initWithFile:[NSString stringWithFormat:@"ch%i_default.png",type]]autorelease]; 


    return creature; 

} 

問題是,當我在我的生物類對象 '生物' 存儲在NSUserDefault using-

- (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"]; 
} 

然後保存生物對象using-

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]; 

的問題是,當我加載此我得到以前存儲的生物的屬性值,但分配給先前生物的圖像可能不會被複制。因爲如果我將newcreature添加到任何CCLayer,它不會顯示任何圖像,但它會獲得先前生物的屬性值。

現在我能做些什麼來獲得具有圖像的新生物?是否需要將圖像名稱添加爲單獨的屬性?

+0

好吧,最可能的答案是 - 需要對屬於某個類的所有內容進行編碼/解碼。所以如果我想要對象的對應圖像,那麼還需要對它進行編碼/解碼。 – russell

回答

2

你可以簡單地存儲類型以及然後像這樣做:

@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:代替initializeCreatureWithType:它是「更可可」。

+0

感謝您的回覆。我知道,但type是不夠的,因爲它是ch%i_default,ch%i_sad,ch%i_happy等可能是需要的,所以需要NSString。然而,這不是主要問題,我有困惑,我是否需要編碼圖像作爲一個單獨的財產。事實上,起初我認爲圖像將被包裹在對象本身中,所以不需要明確地添加它。任何人都可以解釋爲什麼不會發生? – russell

+0

您可以將生成的字符串(例如'ch1_default.png')存儲在實例變量中,並按照我用int顯示的相同方式對其進行編碼/解碼,然後在'initWithCoder:'中調用'self = [self initWithFile :decodedString];'。如果要存儲圖像,請使用[UIImagePNGRepresentation](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/) UIImagePNGRepresentation)將UIImage轉換爲NSData和UIImage imageWithData。 – DarkDust