Skip to content

Instantly share code, notes, and snippets.

@Tantas
Created October 7, 2014 02:20
Show Gist options
  • Save Tantas/99a37937f49b8397d5f2 to your computer and use it in GitHub Desktop.
Save Tantas/99a37937f49b8397d5f2 to your computer and use it in GitHub Desktop.
#import <SpriteKit/SpriteKit.h>
CGFloat DIM_DEFAULT_ZINDEX = 10.0f;
@interface SKScene (Dimmable)
/** Dims everything on the screen below the z-index to the provided color.
Percentage should be between 0.0f and 1.0f.
*/
-(void)dimScreenBelowZIndex:(float)zindex color:(SKColor*)color
percentage:(float)percentage duration:(NSTimeInterval)duration;
/** Removes the dim node from the screen
*/
-(void)removeDimScreen;
/** Dims the screen using predefined settings.
*/
-(void)dimScreenDefault;
@end
#import "SKScene+Dimmable.h"
NSString *SCREEN_DIM_NAME = @"ScreenDimmer";
CGFloat DIM_DEFAULT_PERCENTAGE = 0.6f;
CGFloat DIM_DEFAULT_DURATION = 0.3f;
@implementation SKScene (Dimmable)
- (void)dimScreenBelowZIndex:(float)zindex color:(SKColor*)color
percentage:(float)percentage duration:(NSTimeInterval)duration
{
assert(color != nil && percentage > 0.0f && percentage < 1.0f);
SKSpriteNode* dimSprite = (SKSpriteNode*)[self childNodeWithName:SCREEN_DIM_NAME];
if (!dimSprite) {
dimSprite = [SKSpriteNode spriteNodeWithColor:color size:self.size];
dimSprite.name = SCREEN_DIM_NAME;
dimSprite.zPosition = zindex;
dimSprite.position = CGPointMake(self.size.width/2, self.size.height/2);
dimSprite.alpha = 0.0f;
[self addChild:dimSprite];
}
[dimSprite runAction:[SKAction fadeAlphaTo:percentage duration:duration]];
}
- (void)removeDimScreen
{
[[self childNodeWithName:SCREEN_DIM_NAME] removeFromParent];
}
- (void)dimScreenDefault
{
[self dimScreenBelowZIndex:DIM_DEFAULT_ZINDEX color:[SKColor blackColor]
percentage:DIM_DEFAULT_PERCENTAGE duration:DIM_DEFAULT_DURATION];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment