Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:58:15+00:00 2026-05-28T06:58:15+00:00

I have a strange problem while working with CCSprite subclass Creature. Lets,my object is

  • 0

I have a strange problem while working with CCSprite subclass Creature.

Lets,my object is Creature* creature;

The class Creature declaration-

@interface Creature : CCSprite <NSCoding>{

    int creatureAge;
    NSString *creatureName;
}

Implementation

+(id)initializeCreatureWithType:(int)type
{

    Creature *creature = nil;




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


    return creature;

}

The problem is when i store my Creature class object ‘creature’ in NSUserDefault using-

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

    [encoder encodeObject:self.creatureName forKey:@"creatureName"];
    [encoder encodeInt:self.creatureAge forKey:@"creatureAge"];
}

And the decode it with-

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

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

Then save the creature object using-

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

[defaults setObject:myEncodedObject forKey:@"my_pet"];

And the load-

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 NSData *myEncodedObject = [defaults objectForKey:@"my_pet"];
 Creature* newcreature = (Creature *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];

The problem is that when I load this i get the property value of previously stored creature, but the image that is assigned to previous creature perhaps does not copied. Because if i add the newcreature to any CCLayer it does not display any image, though it get the property value of previous creature.

What can I do now to get the newcreature with image? is it needed to add image name as a separate property???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:58:15+00:00Added an answer on May 28, 2026 at 6:58 am

    You could simply store the type as well and then do it like this:

    @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
    

    You might want to expose creatureType via a property as well. Note that instead of initializeCreatureWithType: it’s “more Cocoa” to use the name creatureWithType:.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a strange problem while running my application. I have a class
I seem to have a strange problem. I am working on a simple dashboard
I have a strange problem with object serialization. in the file documentation it states
I had encountered strange problem while construct a unordeed_set<tuple<int,int>> . I had tried VC++8,
I'm working with MVC recently and I've encountered a strange problem while trying to
I have a strange problem while trying to start JBoss from Eclipse. After starting,
I have one strange problem, if I use graphics.lineTo while moving the mouse (
I have been facing a strange problem while returning dataset through WCF service (WSHttpBinding).
I have strange problem with sharepoint and ajax functionality. We have an UpdatePanel placed
i have strange problem doing reporting: i have numerous clients with different issued invoices.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.