|
11 | 11 | // -----------------------------------------------------------------
|
12 | 12 |
|
13 | 13 | #import "SetupScene.h"
|
| 14 | +#import "cocos2d-ui.h" |
| 15 | +#import "GameTypes.h" |
14 | 16 |
|
15 | 17 | // -----------------------------------------------------------------
|
16 | 18 |
|
17 | 19 | @implementation SetupScene
|
| 20 | +{ |
| 21 | + CCSlider *_soundVolume; |
| 22 | + CCSlider *_musicVolume; |
| 23 | +} |
| 24 | + |
| 25 | +// ----------------------------------------------------------------- |
| 26 | + |
| 27 | +- (instancetype)init |
| 28 | +{ |
| 29 | + self = [super init]; |
| 30 | + |
| 31 | + _soundVolume = [[CCSlider alloc] initWithBackground:[CCSpriteFrame frameWithImageNamed:@"slider.png"] |
| 32 | + andHandleImage:[CCSpriteFrame frameWithImageNamed:@"handle.png"]]; |
| 33 | + _soundVolume.positionType = CCPositionTypeNormalized; |
| 34 | + _soundVolume.position = (CGPoint){0.5, 0.6}; |
| 35 | + _soundVolume.anchorPoint = (CGPoint){0.5, 0.5}; |
| 36 | + _soundVolume.endStop = kGameSliderEndStop; |
| 37 | + [self addChild:_soundVolume]; |
| 38 | + |
| 39 | + CCLabelTTF *soundLabel = [CCLabelTTF labelWithString:@"Sound Volume" fontName:@"ArialMT" fontSize:32]; |
| 40 | + soundLabel.positionType = CCPositionTypeNormalized; |
| 41 | + soundLabel.position = (CGPoint){0.5, 0.55}; |
| 42 | + soundLabel.fontColor = [CCColor colorWithRed:1.00 green:0.65 blue:0.00]; |
| 43 | + [self addChild:soundLabel]; |
| 44 | + |
| 45 | + _musicVolume = [[CCSlider alloc] initWithBackground:[CCSpriteFrame frameWithImageNamed:@"slider.png"] |
| 46 | + andHandleImage:[CCSpriteFrame frameWithImageNamed:@"handle.png"]]; |
| 47 | + _musicVolume.positionType = CCPositionTypeNormalized; |
| 48 | + _musicVolume.position = (CGPoint){0.5, 0.4}; |
| 49 | + _musicVolume.anchorPoint = (CGPoint){0.5, 0.5}; |
| 50 | + _musicVolume.endStop = kGameSliderEndStop; |
| 51 | + [self addChild:_musicVolume]; |
| 52 | + |
| 53 | + CCLabelTTF *musicLabel = [CCLabelTTF labelWithString:@"Music Volume" fontName:@"ArialMT" fontSize:32]; |
| 54 | + musicLabel.positionType = CCPositionTypeNormalized; |
| 55 | + musicLabel.position = (CGPoint){0.5, 0.35}; |
| 56 | + musicLabel.fontColor = [CCColor colorWithRed:1.00 green:0.65 blue:0.00]; |
| 57 | + [self addChild:musicLabel]; |
| 58 | + |
| 59 | + [self validateSetup]; |
| 60 | + |
| 61 | + // load setup |
| 62 | + NSUserDefaults *setup = [NSUserDefaults standardUserDefaults]; |
| 63 | + _soundVolume.sliderValue = [setup floatForKey:kGameKeySoundVolume]; |
| 64 | + _musicVolume.sliderValue = [setup floatForKey:kGameKeyMusicVolume]; |
| 65 | + |
| 66 | + // Ze back button ... |
| 67 | + CCButton *back = [CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"back.png"]]; |
| 68 | + back.positionType = CCPositionTypeNormalized; |
| 69 | + back.position = (CGPoint){0.5,0.1}; |
| 70 | + [back setBlock:^(id sender) |
| 71 | + { |
| 72 | + [[CCDirector sharedDirector] popSceneWithTransition:[CCTransition transitionRevealWithDirection:CCTransitionDirectionDown |
| 73 | + duration:0.5]]; |
| 74 | + }]; |
| 75 | + [self addChild:back]; |
| 76 | + |
| 77 | + return self; |
| 78 | +} |
| 79 | + |
| 80 | +// ----------------------------------------------------------------- |
| 81 | + |
| 82 | +- (void)validateSetup |
| 83 | +{ |
| 84 | + // makes sure there is a valid setup |
| 85 | + NSUserDefaults *setup = [NSUserDefaults standardUserDefaults]; |
| 86 | + // make sure keys exist |
| 87 | + if ([setup objectForKey:kGameKeySoundVolume] == nil) [setup setFloat:1.0 forKey:kGameKeySoundVolume]; |
| 88 | + if ([setup objectForKey:kGameKeyMusicVolume] == nil) [setup setFloat:1.0 forKey:kGameKeyMusicVolume]; |
| 89 | + [setup synchronize]; |
| 90 | +} |
| 91 | + |
| 92 | +// ----------------------------------------------------------------- |
| 93 | + |
| 94 | +- (void)dealloc |
| 95 | +{ |
| 96 | + // save setup on exit |
| 97 | + NSUserDefaults *setup = [NSUserDefaults standardUserDefaults]; |
| 98 | + [setup setFloat:_soundVolume.sliderValue forKey:kGameKeySoundVolume]; |
| 99 | + [setup setFloat:_musicVolume.sliderValue forKey:kGameKeyMusicVolume]; |
| 100 | +} |
18 | 101 |
|
19 | 102 | // -----------------------------------------------------------------
|
20 | 103 |
|
|
0 commit comments