0

Here is the code for my 3 CCSliders, they change the bat color fine, I saved the sliders and those are working fine, but I need to save the _SettingBat.color to load across the app, this is where I am having issues saving the color to NSUserDefaults.

CCColor *batColor;

-(void)redSlider
{

    batColor = [CCColor colorWithRed:_redSlider.sliderValue green:_greenSlider.sliderValue blue:_blueSlider.sliderValue];
    _settingsBat.color = batColor;

}

-(void)greenSlider
{

    batColor = [CCColor colorWithRed:_redSlider.sliderValue green:_greenSlider.sliderValue blue:_blueSlider.sliderValue];
    _settingsBat.color = batColor;

}

-(void)blueSlider
{

    batColor = [CCColor colorWithRed:_redSlider.sliderValue green:_greenSlider.sliderValue blue:_blueSlider.sliderValue];
    _settingsBat.color = batColor;

}

-(void)save
{

    NSLog(@"The Color is, %@",_settingsBat.color);
    [[NSUserDefaults standardUserDefaults] setFloat:_redSlider.sliderValue forKey:@"redSlider"];
    [[NSUserDefaults standardUserDefaults] setFloat:_greenSlider.sliderValue forKey:@"greenSlider"];
    [[NSUserDefaults standardUserDefaults] setFloat:_blueSlider.sliderValue forKey:@"blueSlider"];
    [[NSUserDefaults standardUserDefaults]synchronize];
    _savedLabel.visible = TRUE;

    NSLog(@"My Key is, %@",[[NSUserDefaults standardUserDefaults] stringForKey:@"batColor"]);

}
Jason
  • 650
  • 2
  • 10
  • 33

2 Answers2

0

This works for cocos2D V3

Get a UIColor instance:

UIColor *spriteUIColor = [_spriteColor.color UIColor];

Archive it:

NSData *colorData = [NSKeyedArchiver archivedDataWithRootObject:spriteUIColor];
[[NSUserDefaults standardUserDefaults] setObject:colorData forKey:@"mySavedColor"];

To restore it:

NSData *colorData = [[NSUserDefaults standardUserDefaults] objectForKey:@"mySavedColor"];
UIColor *restoredUIColor = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
lucianomarisi
  • 1,552
  • 11
  • 24
  • I have tested on a blank project with this `UIColor *saveColor = [[CCColor brownColor] UIColor];` instead of `UIColor *spriteUIColor = [_spriteColor.color UIColor];` and it works fine. – lucianomarisi Apr 27 '14 at 22:22
  • Yes that will work, but the color of the sprite is a variable that users will set with 3 rgb CCColor sliders. – Jason Apr 28 '14 at 01:43
  • What about creating an instace of UIColor with whatever rgb you have and saving that? – lucianomarisi Apr 28 '14 at 10:34
  • I've updated my code above to show exactly what I am doing, is there a better way to do this to achieve what I am looking for? – Jason Apr 28 '14 at 12:09
0

I generally disagree with archiving objects as a means to save a simple value. Besides not being very efficient, you also violate separation of data from your implementation (maybe not a big deal here, but...), you can run into issues when the class implementation changes, etc.

I would say to avoid saving the channels individually too, since they are really one value. I would personally save the color as a numeric RGB value then use something like this macro:

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]`

(taken from this SO answer)

to create your UIColor at load time from the stored RGB value.

Read this SO Q/A to understand how the bits need to be packed/unpacked into the 32bit number. Traditionally you do this by using the first byte as the A channel, second as R, third G, and the last is B. In your case it doesn't really matter how you pack them as long as you unpack them the same, but the macro I posted expects ARGB.

I can update my answer with an example of how to store the color as a single 32 bit number if you need; as well as how to reconstitute a UIColor from the number.

Community
  • 1
  • 1
Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • Yes if you could add the full detail, I have searched for this for days, and never seen the source you provided. Would this affect the code for my sliders as well? Also, what does the 16 and 8 represent? Thank you for your reply! – Jason May 07 '14 at 23:54
  • @Jason your sliders should all be connected to the same method since they are *all* identical to one another. there is no sense at all in having to maintain 3 separate duplicate methods. – Brad Allred May 08 '14 at 01:06
  • @Jason not in a position to write code atm, but I updated with a link and explanation to the premise I would use. You would probably learn more doing it yourself anyhow :) – Brad Allred May 08 '14 at 14:50
  • I read your link, but that didn't help me answer my question. Been trying this on my own for a while before posting this question. Still waiting for an answer that actually works. – Jason Jun 02 '14 at 03:31
  • @Jason what are you having problems understanding? My answer and that link contain all the information needed to pack the RGB(A) value of a `UIColor`/`CColor` into 32 bits and then unpack them into arguments for creating another `UIColor` instance. – Brad Allred Jun 02 '14 at 15:10
  • I don't understand how to use that for what I'm doing. You said you disagree archiving simple values, is there a better way? – Jason Jun 04 '14 at 00:20
  • @Jason, I said I disagree with archiving *objects* just to save a 32 bit value that you can *easily* pack into a 32 bit number of the form `0xAARRGGBB` – Brad Allred Jun 04 '14 at 03:12