たまごに書いてある数字を覚えて、順番にたっちしていくiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface EggScene : SKScene
@end
@implementation EggScene
– (void)didMoveToView:(SKView *)view
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
[self createWall];
[self createEggs];
[self createStartButton];
}
– (void)createWall
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.view.bounds];
}
– (void)createEggs
{
for (int i=0; i<5; i++) {
float x = i * 90 + 90;
float y = 200;
SKNode *egg = [self createEgg];
egg.position = CGPointMake(x, y);
SKLabelNode *l = [SKLabelNode node];
l.name = @”number”;
l.text = [@(i + 1) stringValue];
l.fontName = @”MarkerFelt-Thin”;
l.fontSize = 30;
l.fontColor = [SKColor darkGrayColor];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
[egg addChild:l];
}
}
– (SKNode *)createEgg
{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-30, –30, 60, 60)];
SKShapeNode *egg = [SKShapeNode node];
egg.name = @”egg”;
egg.lineWidth = 2;
egg.fillColor = [SKColor whiteColor];
egg.strokeColor = [SKColor lightGrayColor];
egg.path = path.CGPath;
[self addChild:egg];
egg.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
egg.physicsBody.restitution = 0.7;
return egg;
}
– (void)createStartButton
{
SKLabelNode *start = [SKLabelNode node];
start.name = @”start”;
start.text = @”START”;
start.fontName = @”MarkerFelt-Thin”;
start.fontSize = 40;
start.position = CGPointMake(CGRectGetMidX(self.view.bounds), 50);
[self addChild:start];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *start = [self childNodeWithName:@”start”];
if ([start containsPoint:p]) {
SKAction *big = [SKAction scaleTo:1.3 duration:0.1];
SKAction *fade = [SKAction fadeOutWithDuration:0.3];
[start runAction:[SKAction sequence:@[big, fade]] completion:^{
[start removeFromParent];
}];
[self snapEgg];
return;
}
[self enumerateChildNodesWithName:@”egg” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
[[node childNodeWithName:@”number”] runAction:[SKAction fadeInWithDuration:0.5]];
}
}];
}
– (void)snapEgg
{
[self enumerateChildNodesWithName:@”egg” usingBlock:^(SKNode *node, BOOL *stop) {
SKLabelNode *l = (SKLabelNode *)[node childNodeWithName:@”number”];
l.alpha = 0;
float angle = M_PI_4 * (arc4random() % 8);
[node.physicsBody applyImpulse:CGVectorMake(200 * cos(angle), 200 * sin(angle))];
self.physicsWorld.gravity = CGVectorMake(0, –9.8);
}];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[EggScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end