
流れる縞模様からひょっこり出てくる丸をタップしていくiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface StripeScene : SKScene
@property NSTimeInterval startTime;
@property BOOL lockSecret;
@end
@implementation StripeScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [self color:3];
[self createScrollNode];
[self createStripe];
}
– (void)createScrollNode
{
SKNode *scroll = [SKNode node];
scroll.name = @”scroll”;
[self addChild:scroll];
}
– (void)createStripe
{
for (int i=0; i<3; i++) {
SKSpriteNode *stripe = [SKSpriteNode spriteNodeWithColor:[self color:0] size:CGSizeMake(CGRectGetMaxX(self.frame), 60)];
stripe.name = @”stripe”;
stripe.position = CGPointMake(CGRectGetMidX(self.frame), i * 150 + 50);
stripe.zPosition = 4;
[self addChild:stripe];
}
}
– (void)createArrow:(CGPoint)p
{
SKNode *scroll = [self childNodeWithName:@”scroll”];
SKLabelNode *arrow = [SKLabelNode node];
arrow.name = @”arrow”;
arrow.text = @”<“;
arrow.fontSize = 30;
arrow.fontColor = [self color:4];
arrow.zPosition = 5;
[scroll addChild:arrow];
arrow.position = p;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *scroll = [self childNodeWithName:@”scroll”];
CGPoint p = [[touches anyObject] locationInNode:scroll];
[scroll enumerateChildNodesWithName:@”secret” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
[node.physicsBody applyImpulse:CGVectorMake(2, 20)];
[node removeAllActions];
}
}];
}
#define scrollVelocity –20
– (void)update:(NSTimeInterval)currentTime
{
if (self.startTime <= 0) {
self.startTime = currentTime;
}
SKNode *scroll = [self childNodeWithName:@”scroll”];
float x = (currentTime – self.startTime) * scrollVelocity;
scroll.position = CGPointMake(x, self.position.y);
[scroll enumerateChildNodesWithName:@”stripe” usingBlock:^(SKNode *node, BOOL *stop) {
node.position = CGPointMake(160, node.position.y);
}];
if (((int)scroll.position.x) % 50 == 0) {
[self enumerateChildNodesWithName:@”stripe” usingBlock:^(SKNode *node, BOOL *stop) {
CGPoint p = [self convertPoint:CGPointMake(340, node.position.y) toNode:scroll];
[self createArrow:p];
[self createSecretRandom:p];
}];
}
[scroll enumerateChildNodesWithName:@”secret” usingBlock:^(SKNode *node, BOOL *stop) {
CGPoint p = [scroll convertPoint:node.position toNode:self];
if (p.x < –30 || p.y < –50) {
[node removeFromParent];
}
}];
[scroll enumerateChildNodesWithName:@”arrow” usingBlock:^(SKNode *node, BOOL *stop) {
CGPoint p = [scroll convertPoint:node.position toNode:self];
if (p.x < –30) {
[node removeFromParent];
}
}];
}
– (void)createSecretRandom:(CGPoint)p
{
if (self.lockSecret) {
return;
}
BOOL create = (arc4random() % 3) == 0;
if (create) {
self.lockSecret = YES;
[self performSelector:@selector(setLockSecret:) withObject:[NSNumber numberWithBool:NO] afterDelay:2.0];
SKNode *scroll = [self childNodeWithName:@”scroll”];
SKNode *secret = [SKNode node];
secret.name = @”secret”;
secret.position = CGPointMake(p.x, p.y);
[scroll addChild:secret];
SKAction *waite = [SKAction waitForDuration:(arc4random() % 40) * 0.1+2.0];
SKAction *up = [SKAction moveToY:p.y + 30 duration:(arc4random() % 10) * 0.1 + 0.5];
SKAction *down = [SKAction moveToY:p.y duration:(arc4random() % 10) * 0.1 + 0.5];
[secret runAction:[SKAction sequence:@[waite, up, waite, down]]];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:28 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *a = [SKShapeNode node];
a.path = path.CGPath;
a.fillColor = [self color:1];
a.lineWidth = 0;
a.zPosition = 3;
[secret addChild:a];
UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
for (int i=0; i<2; i++) {
SKShapeNode *b = [SKShapeNode node];
b.path = path2.CGPath;
b.fillColor = [self color:1];
b.lineWidth = 0;
b.zPosition = 2;
[secret addChild:b];
SKAction *w = [SKAction waitForDuration:waite.duration + up.duration];
[b runAction:[SKAction sequence:@[w, [SKAction moveByX:i ? 25 : –25 y:25 duration:0.5], w, [SKAction moveTo:CGPointZero duration:0.5]]]];
}
}
}
#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
– (SKColor *)color:(NSInteger)i
{
switch (i) {
case 0:
return ColorHex(0x007148);
case 1:
return ColorHex(0x60A859);
case 2:
return ColorHex(0x9BDA6A);
case 3:
return ColorHex(0xC7F774);
case 4:
return ColorHex(0xF9FFEF);
default:
break;
}
return nil;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[StripeScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end