Skip to content

Commit 1e69a51

Browse files
committed
Demo finished
1 parent c77d3ea commit 1e69a51

File tree

11 files changed

+99
-9
lines changed

11 files changed

+99
-9
lines changed

cocos2d-ui/CCSlider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
*/
6969
@property (nonatomic,assign) float sliderValue;
7070

71+
@property (nonatomic, assign) float endStop;
72+
7173
#pragma mark Customizing the Appearance of the Slider
7274

7375
/// -----------------------------------------------------------------------

cocos2d-ui/CCSlider.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ - (id) initWithBackground:(CCSpriteFrame*)background andHandleImage:(CCSpriteFra
3232
_backgroundSpriteFrames = [[NSMutableDictionary alloc] init];
3333
_handleSpriteFrames = [[NSMutableDictionary alloc] init];
3434

35+
_endStop = 0.0;
36+
3537
if (background)
3638
{
3739
_background = [CCSprite9Slice spriteWithSpriteFrame:background];
@@ -68,7 +70,7 @@ - (void) updateSliderPositionFromValue
6870
{
6971
CGSize size = [self convertContentSizeToPoints: self.preferredSize type:self.preferredSizeType];
7072

71-
_handle.position = ccp(size.width * _sliderValue, size.height/2.0f);
73+
_handle.position = ccp(_endStop + ((size.width - (2 * _endStop)) * _sliderValue), size.height * 0.5);
7274
}
7375

7476
#if __CC_PLATFORM_IOS || __CC_PLATFORM_ANDROID
@@ -185,10 +187,10 @@ - (void) inputDraggedWithPos:(CGPoint)dragPos
185187
delta.y = 0;
186188

187189
CGPoint newPos = ccpAdd(_handleStartPos, delta);
188-
if (newPos.x < 0) newPos.x = 0;
189-
if (newPos.x >= sizeInPoints.width) newPos.x = sizeInPoints.width;
190+
if (newPos.x < _endStop) newPos.x = _endStop;
191+
if (newPos.x >= (sizeInPoints.width - _endStop)) newPos.x = sizeInPoints.width - _endStop;
190192

191-
_sliderValue = newPos.x / sizeInPoints.width;
193+
_sliderValue = (newPos.x - _endStop) / (sizeInPoints.width - (2 * _endStop));
192194
if (self.continuous && _sliderValue != _dragStartValue)
193195
{
194196
_dragStartValue = _sliderValue;

templates/cocos2d iOS demo.xctemplate/Classes/GameTypes.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919
#define kGamePaddleSpeed 800
2020
#define kGameBallSpeed 1200
2121

22-
#define kGameSpinFactor 0.2
23-
#define kGameSpinRandomFactor 0.1
24-
25-
22+
#define kGameSpinFactor 0.25
23+
#define kGameSpinRandomFactor 0.15
2624

25+
#define kGameSliderEndStop 15
2726

27+
#define kGameKeySoundVolume @"sound.volume"
28+
#define kGameKeyMusicVolume @"music.volume"
2829

2930

3031

templates/cocos2d iOS demo.xctemplate/Classes/mainScene.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ - (id)init
8989
button.position = ccp(0.5, 0.4);
9090
[button setBlock:^(id sender)
9191
{
92-
92+
[[CCDirector sharedDirector] pushScene:[SetupScene new]
93+
withTransition:[CCTransition transitionRevealWithDirection:CCTransitionDirectionUp
94+
duration:0.5]];
9395
}];
9496
[self addChild:button];
9597

templates/cocos2d iOS demo.xctemplate/Classes/setupScene.m

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,93 @@
1111
// -----------------------------------------------------------------
1212

1313
#import "SetupScene.h"
14+
#import "cocos2d-ui.h"
15+
#import "GameTypes.h"
1416

1517
// -----------------------------------------------------------------
1618

1719
@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+
}
18101

19102
// -----------------------------------------------------------------
20103

2.92 KB
Loading
3.09 KB
Loading
3.32 KB
Loading
3.3 KB
Loading
4.03 KB
Loading

0 commit comments

Comments
 (0)