Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created August 26, 2017 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/0e6258c0162d8b98a8a01b30e0d944be to your computer and use it in GitHub Desktop.
Save mzsima/0e6258c0162d8b98a8a01b30e0d944be to your computer and use it in GitHub Desktop.
pineapple ring
//
// ViewController.m
// pineappleRing
//
// Created by MizushimaYusuke on 2017/08/26.
// Copyright © 2017 MizushimaYusuke. All rights reserved.
//
#import "ViewController.h"
@import SpriteKit;
@interface ViewController ()
@property (nonatomic, weak) SKScene *scene;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createPineapples];
}
- (void)setupScene {
SKView *sv = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *s = [SKScene sceneWithSize:sv.frame.size];
s.backgroundColor = [UIColor colorWithHue:0.4 saturation:0.1 brightness:1 alpha:1];
[sv presentScene:s];
[self.view addSubview:sv];
self.scene = s;
}
- (void)createPineapples {
for (int i=0; i<18; i++) {
for (int j=0; j<8; j++) {
if (arc4random_uniform(10) == 0) {
continue;
}
SKNode *pineapple = [self createPineapple];
pineapple.position = CGPointMake(i * 50, j * 50);
}
}
}
- (SKNode *)createPineapple {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 40), NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor colorWithHue:0.14 saturation:0.8 brightness:0.95 alpha:1] set];
CGContextFillEllipseInRect(ctx, CGRectMake(0, 0, 40, 40));
CGContextTranslateCTM(ctx, 20, 20);
[[UIColor colorWithHue:0.14 saturation:0.2 brightness:1 alpha:1] set];
for (int i=0; i<10; i++) {
float r0 = arc4random_uniform(2);
float r1 = arc4random_uniform(5) + 4;
CGContextFillRect(ctx, CGRectMake(10 + r0, -0.5, r1, 1));
CGContextRotateCTM(ctx, M_PI / 5);
}
CGContextSetBlendMode(ctx, kCGBlendModeClear);
CGContextFillEllipseInRect(ctx, CGRectMake(-8, -8, 16, 16));
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
SKTexture *texture = [SKTexture textureWithImage:img];
SKSpriteNode *pineapple = [SKSpriteNode spriteNodeWithTexture:texture];
pineapple.name = @"pineapple";
pineapple.position = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
[self.scene addChild:pineapple];
return pineapple;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.scene enumerateChildNodesWithName:@"pineapple" usingBlock:^(SKNode * _Nonnull node, BOOL * _Nonnull stop) {
if (arc4random_uniform(5) == 0) {
[node runAction:[SKAction rotateByAngle:2.0 * M_PI duration:3.0]];
}
}];
}
@end
@mzsima
Copy link
Author

mzsima commented Aug 26, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment