数字をタッチして、スタンプを10個埋めて遊ぶiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface StampScene : SKScene
@property (nonatomic, strong) NSMutableArray *stampGrid;
@property (nonatomic, strong) NSMutableArray *coins;
@end
@implementation StampScene
-(void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor colorWithRed:0.8 green:0.8 blue:0.1 alpha:1];
[self createGrid];
[self createCoin];
}
– (void)createGrid
{
self.stampGrid = [NSMutableArray array];
for (int i=0; i<10; i++) {
SKSpriteNode *grid = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(40, 40)];
grid.position = CGPointMake(i * 44 + 40, CGRectGetMaxY(self.frame) – 60);
[self addChild:grid];
SKLabelNode *l = [SKLabelNode node];
l.fontSize = 15;
l.fontColor = self.backgroundColor;
l.text = [NSString stringWithFormat:@”%d”, i+1];
l.position = CGPointMake(14, –16);
l.zRotation = M_PI/5.0;
[grid addChild:l];
[self.stampGrid addObject:grid];
}
}
– (void)createCoin
{
self.coins = [NSMutableArray array];
NSMutableArray *num = [@[@”1″, @”2″, @”3″, @”4″, @”5″, @”6″, @”7″] mutableCopy];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
for (int i=0; i<7; i++) {
SKShapeNode *coin = [SKShapeNode node];
coin.path = path.CGPath;
float x = i * 60 + 60;
float y = 60 * sin(i * M_PI / 3.0) + 100;
coin.position = CGPointMake(x, y);
coin.fillColor = [SKColor colorWithHue:i*0.1 saturation:0.7 brightness:0.7 alpha:1];
[self addChild:coin];
SKLabelNode *l = [SKLabelNode node];
l.text = num[arc4random() % num.count];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
[coin addChild:l];
[num removeObject:l.text]; // for random array
coin.name = l.text;
[self.coins addObject:coin];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self.coins enumerateObjectsUsingBlock:^(SKShapeNode *obj, NSUInteger idx, BOOL *stop) {
if ([obj containsPoint:p]) {
SKAction *big = [SKAction scaleTo:1.5 duration:0.2];
SKAction *fade = [SKAction fadeAlphaTo:0.1 duration:0.2];
[obj runAction:[SKAction sequence:@[big, fade]]];
[self stamp:obj.name color:obj.fillColor];
}
}];
}
– (void)stamp:(NSString *)str color:(SKColor *)color
{
int count = 0;
for (int i=0; i<self.stampGrid.count; i++) {
SKNode *grid = self.stampGrid[i];
if (![grid.name isEqual:@”stamp”]) {
SKSpriteNode *stamp = [SKSpriteNode spriteNodeWithColor:[color colorWithAlphaComponent:0.6] size:CGSizeMake(30, 30)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * count * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[grid addChild:stamp];
});
count++;
grid.name = @”stamp”;
if (count+1 > [str intValue]) {
return;
}
}
}
int stampCount = [[[self.stampGrid filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”name = ‘stamp'”]] valueForKeyPath:@”@count”] intValue];
if (stampCount >= 10) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.children enumerateObjectsUsingBlock:^(SKNode *n, NSUInteger idx, BOOL *stop) {
[n removeFromParent];
}];
[self createGrid];
[self createCoin];
});
}
}
@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 = [[StampScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end