タップで丸が割れて次の数が出てくるカウンタiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface OpenScene : SKScene
@property (nonatomic) NSUInteger counter;
@end
@implementation OpenScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [UIColor colorWithWhite:0.98 alpha:1];
}
– (void)createCounterAt:(int)num
{
SKColor *color = [SKColor colorWithHue:0.15 * num saturation:0.8 brightness:1.0 alpha:1];
for (int i=0; i<2; i++) {
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:60 startAngle:0 endAngle:M_PI clockwise:i ? NO : YES];
SKShapeNode *n = [SKShapeNode node];
n.name = i ? @”bottom” : @”top”;
n.position = CGPointMake(160, CGRectGetMidY(self.frame));
n.path = path.CGPath;
n.lineWidth = 30;
n.fillColor = [[SKColor whiteColor] colorWithAlphaComponent:0.7];
n.strokeColor = [color colorWithAlphaComponent:0.8];
n.zPosition = -num;
[self addChild:n];
if (i == 0) {
SKLabelNode *l = [SKLabelNode node];
l.text = [@(num) stringValue];
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
l.fontColor = color;
[n addChild:l];
} else {
for (int j=0; j<num; j++) {
SKSpriteNode *dot = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(5, 5)];
int idx = (j+1) / 2;
dot.position = CGPointMake((j%2) ? idx*10 : -idx*10, – 20);
[n addChild:dot];
}
}
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self enumerateChildNodesWithName:@”top” usingBlock:^(SKNode *node, BOOL *stop) {
[node runAction:[SKAction moveByX:0 y:40 duration:0.2]];
}];
[self enumerateChildNodesWithName:@”bottom” usingBlock:^(SKNode *node, BOOL *stop) {
[node runAction:[SKAction moveByX:0 y:-40 duration:0.2]];
}];
self.counter++;
[self createCounterAt:self.counter];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[OpenScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end