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 7752297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:45:49+00:00 2026-06-01T11:45:49+00:00

Im am using a pattern from the book Learning Cocos2D. I have a GameState

  • 0

Im am using a pattern from the book Learning Cocos2D. I have a GameState class that is a singleton that can be saved as a state. The BOOL soundOn gets init as false the first time the class gest created, but the array levelProgress dosent. I made comments on all the lines on which I have tried to init the array.
The class uses a helper that loads and saves the data.
When i try 1 or 2 i get “instance variable ‘levelProgress’ accessed in class method” error.

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface GameState : NSObject <NSCoding> {
    BOOL soundOn;
    NSMutableArray *levelProgress; 
}

+ (GameState *) sharedInstance;
- (void)save;

@property (assign) BOOL soundOn;
@property (nonatomic, retain) NSMutableArray *levelProgress;
@end


#import "GameState.h"
#import "GCDatabase.h"

@implementation GameState

@synthesize soundOn;
@synthesize levelProgress;

static GameState *sharedInstance = nil;

+(GameState*)sharedInstance {
    @synchronized([GameState class]) 
    {
        if(!sharedInstance) {
            sharedInstance = [loadData(@"GameState") retain];
            if (!sharedInstance) {
                [[self alloc] init]; 
                // 1. levelProgress = [[NSMutableArray alloc] init];
            }
        }
        return sharedInstance; 
    }
    return nil; 
}

+(id)alloc 
{
    @synchronized ([GameState class])
    {        
        NSAssert(sharedInstance == nil, @"Attempted to allocate a \
                 second instance of the GameState singleton"); 
        sharedInstance = [super alloc];
        // 2. levelProgress = [[NSMutableArray alloc] init];
        return sharedInstance; 
    }
    return nil;  
}

- (void)save {
    saveData(self, @"GameState");
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    // 3. levelProgress = [[NSMutableArray alloc] init];
    [encoder encodeBool:currentChoosenCountry forKey:@"soundOn"];
    [encoder encodeObject:levelProgress forKey:@"levelProgress"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if ((self = [super init])) {
        // 4. levelProgress = [[NSMutableArray alloc] init];    
        soundOn = [decoder decodeBoolForKey:@"soundOn"];
        levelProgress = [decoder decodeObjectForKey:@"levelProgress"];
    }
    return self;
}
@end

Solution:
*I just added av init method…*

-(id)init {
    self = [super init];
    if (self != nil) {
        levelProgress = [[NSMutableArray alloc] init];
    }
    return self;
}
  • 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-06-01T11:45:50+00:00Added an answer on June 1, 2026 at 11:45 am

    Try this (this code may contain syntax or logic errors – I wrote it in notepad):

    @interface GameState : NSObject <NSCoding>
    
    @property (nonatomic, readwrite) BOOL soundOn;
    @property (nonatomic, retain) NSMutableArray *levelProgress;
    
    + (GameState *)sharedState;
    - (void)writeDataToCache;
    
    @end
    

    //

    @implementation GameState
    
    @synthesize soundOn, levelProgress;
    
    #pragma mark - Singleton
    
    static GameState *sharedState = nil;
    
    + (void)initialize {
        static BOOL initialized = NO;
        if (!initialized) {
            initialized = YES;
            if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/gameState", pathCache]]) {
                NSData *encodedObject = [[NSData alloc] initWithContentsOfFile:[NSString stringWithFormat:@"%@/gameState", pathCache]];
                data = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
            }
            else
                data = [[GameState alloc] init];
        }
    }
    + (GameState *)sharedState {
        return sharedState;
    }
    
    #pragma mark - Initialization
    
    - (id)init {
        if (self = [super init]) { // will be inited while application first run
            soundOn = NO;
            levelProgress = [[NSMutableArray alloc] init];
    
            return self;
        }
        return nil;
    }
    
    #pragma mark - Coding Implementation
    
    - (void)writeDataToCache { // use this method to save current state to cache
        NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:self];
        if([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/GameState", pathCache]])
            [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/GameState", pathCache] error:nil];
        [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@/GameState", pathCache] contents:encodedObject attributes:nil];
        NSLog(@"GameState was saved successfully.");
    }
    - (void)encodeWithCoder:(NSCoder*)encoder {
        [encoder encodeBool:self.soundOn forKey:@"soundOn"];
        [encoder encodeObject:self.levelProgress forKey:@"levelProgress"];
    }
    - (id)initWithCoder:(NSCoder*)decoder {
        if ((self = [super init])) {
            self.soundOn = [decoder decodeBoolForKey:@"soundOn"];
            self.levelProgress = [decoder decodeObjectForKey:@"levelProgress"];
    
            NSLog(@"GameState was inited successfully");
            return self;
        }
        return nil;
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented a DAL using Rob Conery's spin on the repository pattern (from
From Head First design patterns book, the singleton pattern with double checked locking has
We're using the DTO pattern to marshal our domain objects from the service layer
From what I've seen about using the Async CTP with the event asynchronous pattern,
Is there a pattern using Linq to dynamically create a filter? I have the
I am trying to extract the below pattern from a string using Ruby and
I need simple hook for mercurial that checks commit comment using pattern. Here is
Is singleton a design pattern or a design antipattern? From here http://neugierig.org/software/chromium/notes/ , it
I am using the pattern from NerdDinner. I call Index() in my test Method
I've been using a pattern in an application where I'm setting arbitrary attributes on

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.