Skip to content

Commit

Permalink
Demo finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkemose committed Jul 22, 2015
1 parent c77d3ea commit 1e69a51
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 9 deletions.
2 changes: 2 additions & 0 deletions cocos2d-ui/CCSlider.h
Expand Up @@ -68,6 +68,8 @@
*/
@property (nonatomic,assign) float sliderValue;

@property (nonatomic, assign) float endStop;

#pragma mark Customizing the Appearance of the Slider

/// -----------------------------------------------------------------------
Expand Down
10 changes: 6 additions & 4 deletions cocos2d-ui/CCSlider.m
Expand Up @@ -32,6 +32,8 @@ - (id) initWithBackground:(CCSpriteFrame*)background andHandleImage:(CCSpriteFra
_backgroundSpriteFrames = [[NSMutableDictionary alloc] init];
_handleSpriteFrames = [[NSMutableDictionary alloc] init];

_endStop = 0.0;

if (background)
{
_background = [CCSprite9Slice spriteWithSpriteFrame:background];
Expand Down Expand Up @@ -68,7 +70,7 @@ - (void) updateSliderPositionFromValue
{
CGSize size = [self convertContentSizeToPoints: self.preferredSize type:self.preferredSizeType];

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

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

CGPoint newPos = ccpAdd(_handleStartPos, delta);
if (newPos.x < 0) newPos.x = 0;
if (newPos.x >= sizeInPoints.width) newPos.x = sizeInPoints.width;
if (newPos.x < _endStop) newPos.x = _endStop;
if (newPos.x >= (sizeInPoints.width - _endStop)) newPos.x = sizeInPoints.width - _endStop;

_sliderValue = newPos.x / sizeInPoints.width;
_sliderValue = (newPos.x - _endStop) / (sizeInPoints.width - (2 * _endStop));
if (self.continuous && _sliderValue != _dragStartValue)
{
_dragStartValue = _sliderValue;
Expand Down
9 changes: 5 additions & 4 deletions templates/cocos2d iOS demo.xctemplate/Classes/GameTypes.h
Expand Up @@ -19,12 +19,13 @@
#define kGamePaddleSpeed 800
#define kGameBallSpeed 1200

#define kGameSpinFactor 0.2
#define kGameSpinRandomFactor 0.1


#define kGameSpinFactor 0.25
#define kGameSpinRandomFactor 0.15

#define kGameSliderEndStop 15

#define kGameKeySoundVolume @"sound.volume"
#define kGameKeyMusicVolume @"music.volume"



Expand Down
4 changes: 3 additions & 1 deletion templates/cocos2d iOS demo.xctemplate/Classes/mainScene.m
Expand Up @@ -89,7 +89,9 @@ - (id)init
button.position = ccp(0.5, 0.4);
[button setBlock:^(id sender)
{

[[CCDirector sharedDirector] pushScene:[SetupScene new]
withTransition:[CCTransition transitionRevealWithDirection:CCTransitionDirectionUp
duration:0.5]];
}];
[self addChild:button];

Expand Down
83 changes: 83 additions & 0 deletions templates/cocos2d iOS demo.xctemplate/Classes/setupScene.m
Expand Up @@ -11,10 +11,93 @@
// -----------------------------------------------------------------

#import "SetupScene.h"
#import "cocos2d-ui.h"
#import "GameTypes.h"

// -----------------------------------------------------------------

@implementation SetupScene
{
CCSlider *_soundVolume;
CCSlider *_musicVolume;
}

// -----------------------------------------------------------------

- (instancetype)init
{
self = [super init];

_soundVolume = [[CCSlider alloc] initWithBackground:[CCSpriteFrame frameWithImageNamed:@"slider.png"]
andHandleImage:[CCSpriteFrame frameWithImageNamed:@"handle.png"]];
_soundVolume.positionType = CCPositionTypeNormalized;
_soundVolume.position = (CGPoint){0.5, 0.6};
_soundVolume.anchorPoint = (CGPoint){0.5, 0.5};
_soundVolume.endStop = kGameSliderEndStop;
[self addChild:_soundVolume];

CCLabelTTF *soundLabel = [CCLabelTTF labelWithString:@"Sound Volume" fontName:@"ArialMT" fontSize:32];
soundLabel.positionType = CCPositionTypeNormalized;
soundLabel.position = (CGPoint){0.5, 0.55};
soundLabel.fontColor = [CCColor colorWithRed:1.00 green:0.65 blue:0.00];
[self addChild:soundLabel];

_musicVolume = [[CCSlider alloc] initWithBackground:[CCSpriteFrame frameWithImageNamed:@"slider.png"]
andHandleImage:[CCSpriteFrame frameWithImageNamed:@"handle.png"]];
_musicVolume.positionType = CCPositionTypeNormalized;
_musicVolume.position = (CGPoint){0.5, 0.4};
_musicVolume.anchorPoint = (CGPoint){0.5, 0.5};
_musicVolume.endStop = kGameSliderEndStop;
[self addChild:_musicVolume];

CCLabelTTF *musicLabel = [CCLabelTTF labelWithString:@"Music Volume" fontName:@"ArialMT" fontSize:32];
musicLabel.positionType = CCPositionTypeNormalized;
musicLabel.position = (CGPoint){0.5, 0.35};
musicLabel.fontColor = [CCColor colorWithRed:1.00 green:0.65 blue:0.00];
[self addChild:musicLabel];

[self validateSetup];

// load setup
NSUserDefaults *setup = [NSUserDefaults standardUserDefaults];
_soundVolume.sliderValue = [setup floatForKey:kGameKeySoundVolume];
_musicVolume.sliderValue = [setup floatForKey:kGameKeyMusicVolume];

// Ze back button ...
CCButton *back = [CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"back.png"]];
back.positionType = CCPositionTypeNormalized;
back.position = (CGPoint){0.5,0.1};
[back setBlock:^(id sender)
{
[[CCDirector sharedDirector] popSceneWithTransition:[CCTransition transitionRevealWithDirection:CCTransitionDirectionDown
duration:0.5]];
}];
[self addChild:back];

return self;
}

// -----------------------------------------------------------------

- (void)validateSetup
{
// makes sure there is a valid setup
NSUserDefaults *setup = [NSUserDefaults standardUserDefaults];
// make sure keys exist
if ([setup objectForKey:kGameKeySoundVolume] == nil) [setup setFloat:1.0 forKey:kGameKeySoundVolume];
if ([setup objectForKey:kGameKeyMusicVolume] == nil) [setup setFloat:1.0 forKey:kGameKeyMusicVolume];
[setup synchronize];
}

// -----------------------------------------------------------------

- (void)dealloc
{
// save setup on exit
NSUserDefaults *setup = [NSUserDefaults standardUserDefaults];
[setup setFloat:_soundVolume.sliderValue forKey:kGameKeySoundVolume];
[setup setFloat:_musicVolume.sliderValue forKey:kGameKeyMusicVolume];
}

// -----------------------------------------------------------------

Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1e69a51

Please sign in to comment.