ぐるぐるまきの太陽をくるっと回すiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface TwirlScene : SKScene
@property (nonatomic, strong) NSMutableArray *flareArr;
@end
@implementation TwirlScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [UIColor colorWithRed:0.7 green:0.9 blue:1.0 alpha:1];
[self createSun];
[self createFlare];
[self createButton];
}
#define SunColor [SKColor colorWithRed:0.8 green:0.2 blue:0.2 alpha:1]
– (void)createSun
{
float angle = M_PI / 2.5;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
for (int i=0; i<15; i++) {
float r = i * 6;
float x = r * cos(angle);
float y = r * sin(angle);
[path addLineToPoint:CGPointMake(x, y)];
angle += M_PI / 2.5;
}
SKShapeNode *sun = [SKShapeNode node];
sun.name = @”sun”;
sun.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
sun.path = path.CGPath;
sun.lineWidth = 3;
sun.strokeColor = SunColor;
[self addChild:sun];
}
– (void)createFlare
{
float r = 110;
float angle = M_PI / 5.0;
self.flareArr = [NSMutableArray array];
SKNode *flareNode = [SKNode node];
flareNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
flareNode.name = @”flareNode”;
[self addChild:flareNode];
for (int i=0; i<10; i++) {
float x = r * cos(angle * i);
float y = r * sin(angle * i);
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:SunColor size:CGSizeMake(40, 5)];
n.name = [NSString stringWithFormat:@”flare%d”, i];
n.position = CGPointMake(x, y);
n.zRotation = angle * i;
[flareNode addChild:n];
[self.flareArr addObject:n];
}
}
– (void)createButton
{
SKLabelNode *btn = [SKLabelNode node];
btn.name = @”button”;
btn.position = CGPointMake(CGRectGetMidX(self.frame), 30);
btn.fontSize = 40;
btn.fontColor = [SKColor colorWithRed:0 green:0.3 blue:0 alpha:1];
[btn setText:@”Twirl”];
[self addChild:btn];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *btn = [self childNodeWithName:@”button”];
SKNode *sun = [self childNodeWithName:@”sun”];
SKNode *flareNode = [self childNodeWithName:@”flareNode”];
if ([btn containsPoint:p]) {
SKAction *twirlA = [SKAction rotateByAngle:-M_PI duration:30.0];
[sun runAction:twirlA];
SKAction *twirlB = [SKAction rotateByAngle:M_PI duration:15.0];
[flareNode runAction:twirlB];
SKAction *fadeIn = [SKAction fadeInWithDuration:0.4];
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.4];
for (int i=0; i<self.flareArr.count; i++) {
if (i%2) {
[self.flareArr[i] runAction:[SKAction repeatActionForever:[SKAction sequence:@[fadeIn, fadeOut]]]];
} else {
[self.flareArr[i] runAction:[SKAction repeatActionForever:[SKAction sequence:@[fadeOut, fadeIn]]]];
}
}
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[TwirlScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end