Search code examples
objective-cdelegatessprite-kitnsuserdefaultsskscene

Storing information, like alphas and which methods were called, in Spritekit


I am still amateur. I want to save information in the SettingsScene when I press the home button. This information includes alpha of each button and the game should know that the methods like defaultModePlay (check sendInformationAboutDefaultChallenge and SettingsScene.h) were called. I hope you can follow my code below. Please bear with me if this code looks silly, your advice is highly welcomed.

In simpler terms: I want to store information (maybe using NSUserDefaults) from the SettingsScene. Is there any way I can do that?

This is how the delegate is set in the SettingsScene...

@class SettingsScene;

@protocol SettingsSceneDelegate <NSObject>

@required

-(void)defaultModePlay;
-(void)mediumModePlay;
-(void)hardModePlay;

@end

@interface SettingsScene : SKScene

@property (nonatomic, weak) id <SettingsSceneDelegate, SKSceneDelegate> delegate;

@end

this is a method in SettingsScene.m

-(void)sendInformationAboutDefaultChallenge
{
    [self.delegate defaultModePlay];
}

touchBegan method in game SettingsScene.m and these are buttons that when pressed, their alphas change and they should call methods that are set in the SettingsScene.h.These methods send information to gameScene which control the speed of particles.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"DefaultChallengeMode"]) {
        defaultChallengeButton.alpha = 0.5;
        mediumChallengeButton.alpha = 1.0;
        hardChallengeButton.alpha = 1.0;

        // the game should play the default way of playing.

        [self sendInformationAboutDefaultChallenge];
    }
    if ([node.name isEqualToString:@"MediumChallengeMode"]) {
        mediumChallengeButton.alpha = 0.5;
        defaultChallengeButton.alpha = 1.0;
        hardChallengeButton.alpha = 1.0;

    }
    if ([node.name isEqualToString:@"hardChallengingMode"]) {
        hardModeButton.alpha = 0.5;
        defaultModeButton.alpha = 1.0;
        mediumModeButton.alpha = 1.0;

    }

    if ([node.name isEqualToString:@"home"]) {

        // save what ever happened in this scene

    }

}

In the gameScene, I have the method from SettingsScene...

-(void)defaultModePlay
{
    defaultGame = YES;
}

Maybe I should store information from the gameScene, I don't know. I have not shown these methods here..., but I hope you can follow...

-(void)mediumModePlay;
-(void)hardModePlay;

Solution

  • Instead of trying to capture the state of every UI element, why not simply store the difficulty mode and use that to determine how the UI should be displayed? I would first put the difficulty modes into an enum (see this article for the best way to do that) and then store them in NSUserDefaults. That will look something like [[NSUserDefaults standardUserDefaults] setInteger:difficultyLevel forKey:@"difficultyLevel"].