Created
February 19, 2014 00:56
-
-
Save zaneclaes/9084090 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AMProgressSpinnerComponent.m | |
// AftermathCore | |
// | |
// Created by Zane Claes on 8/29/13. | |
// Copyright (c) 2013 inZania LLC. All rights reserved. | |
// | |
#import "AMProgressSpinnerComponent.h" | |
#import "AMAssetManager.h" | |
#import "CCSprite+Aftermath.h" | |
#import "cocos2d.h" | |
#define kRadius 40 | |
@implementation AMProgressSpinnerComponent | |
- (void)setOpacity:(GLubyte)opacity { | |
[super setOpacity:opacity]; | |
for(CCSprite *node in self.children) { | |
node.opacity = opacity; | |
} | |
} | |
- (BOOL)isSpinning { | |
return self.numberOfRunningActions>0; | |
} | |
- (void)spin { | |
if([self isSpinning]) { | |
return; | |
} | |
float per = M_PI*2/self.children.count; | |
NSArray *children = self.children.getNSArray?:@[]; | |
[self stopAllActions]; | |
for(NSInteger x=0; x<children.count; x++) { | |
CCSprite *sprite = children[x]; | |
[sprite stopAllActions]; | |
sprite.position = ccp(0,0); | |
float radians = x * per; | |
float radius = kRadius - sprite.contentSize.width/2; | |
float yPos = sinf(radians) * radius; | |
float xPos = cosf(radians) * radius; | |
id action = [CCMoveTo actionWithDuration:0.2 position:ccp(xPos, yPos)]; | |
[sprite runAction:action]; | |
action = [CCRotateBy actionWithDuration:0.05 angle:-M_PI*2]; | |
action = [CCRepeatForever actionWithAction:action]; | |
[sprite runAction:action]; | |
} | |
id action = [CCRotateBy actionWithDuration:0.05 angle:M_PI*2]; | |
action = [CCRepeatForever actionWithAction:action]; | |
[self runAction:action]; | |
} | |
- (void)addOrb:(NSString*)color { | |
NSString *fn = [NSString stringWithFormat:@"orb_%@",color]; | |
CCSprite *sprite = [CCSprite spriteWithAsset:fn]; | |
[self addChild:sprite]; | |
} | |
- (void)prepareForReuse { | |
self.opacity = 0xFF; | |
[self stopAllActions]; | |
[self removeFromParent]; | |
} | |
- (id)init { | |
if((self = [super init])) { | |
[self addOrb:@"white"]; | |
[self addOrb:@"green"]; | |
[self addOrb:@"blue"]; | |
[self addOrb:@"red"]; | |
[self addOrb:@"black"]; | |
self.contentSize = CGSizeMake(kRadius*2, kRadius*2); | |
} | |
return self; | |
} | |
static AMProgressSpinnerComponent *_sharedSpinner; | |
+ (id)sharedSpinner { | |
if(!_sharedSpinner) { | |
_sharedSpinner = [AMProgressSpinnerComponent node]; | |
} | |
return _sharedSpinner; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment