カーリングっぽいシンプルなiPhoneアプリのサンプルコードを描いてみます。
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface CurlingScene : SKScene
@end
@implementation CurlingScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
[self createSceneContents];
}
– (void)createSceneContents
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.physicsWorld.speed = 0.2;
[self createCircle];
[self createStone];
[self createLevelBar];
}
– (void)createCircle
{
for (int i=0; i<2; i++) {
float r = 25 + i * 35;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-r, -r, 2.0*r, 2.0*r)];
SKShapeNode *circle = [SKShapeNode node];
circle.strokeColor = i ? [SKColor blueColor] : [SKColor redColor];
circle.lineWidth = 15;
circle.path = path.CGPath;
circle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 100);
[self addChild:circle];
}
}
– (void)createStone
{
float r = 30;
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-r, -r, 2.0*r, 2.0*r)];
SKShapeNode *stone = [SKShapeNode node];
stone.name = @”stone”;
stone.position = CGPointMake(CGRectGetMidX(self.frame), 80);
stone.path = path.CGPath;
stone.fillColor = [SKColor darkGrayColor];
[self addChild:stone];
stone.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:r];
r = 20;
path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-r, -r, 2.0*r, 2.0*r)];
SKShapeNode *cover = [SKShapeNode node];
cover.position = [self convertPoint:stone.position toNode:stone];
cover.path = path.CGPath;
cover.fillColor = [SKColor redColor];
[stone addChild:cover];
SKShapeNode *handle = [SKShapeNode node];
handle.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 6, 20)].CGPath;
handle.position = CGPointMake(cover.position.x – 3, cover.position.y – 2);
handle.fillColor = [SKColor redColor];
[stone addChild:handle];
}
– (void)createLevelBar
{
SKSpriteNode *gray = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(200, 30)];
gray.position = CGPointMake(CGRectGetMidX(self.frame), 30);
[self addChild:gray];
SKSpriteNode *orange = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(100, 30)];
orange.position = CGPointMake(CGRectGetMidX(self.frame), 30);
[self addChild:orange];
SKSpriteNode *green = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(20, 30)];
green.position = CGPointMake(CGRectGetMidX(self.frame), 30);
[self addChild:green];
SKSpriteNode *needle = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(4, 25)];
needle.name = @”needle”;
needle.position = CGPointMake(0, 30);
[self addChild:needle];
SKAction *moveF = [SKAction moveToX:260 duration:1.0];
SKAction *moveB = [SKAction moveToX:60 duration:1.0];
SKAction *move = [SKAction repeatActionForever:[SKAction sequence:@[moveF, moveB]]];
[needle runAction:move];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *needle = [self childNodeWithName:@”needle”];
SKNode *stone = [self childNodeWithName:@”stone”];
float f = (needle.position.x – 160) * 100 + 15000;
[stone.physicsBody applyForce:CGVectorMake(0, f)];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *stone = [self childNodeWithName:@”stone”];
if (stone.physicsBody.velocity.dy > 1) {
// simulated friction
[stone.physicsBody applyForce:CGVectorMake(0, –60)];
}
[self enumerateChildNodesWithName:@”stop” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.physicsBody.velocity.dy > 1) {
// simulated friction
[node.physicsBody applyForce:CGVectorMake(0, –40)];
}
}];
if (stone.physicsBody.velocity.dy < 1 && stone.position.y > 100) {
stone.name = @”stop”;
[self createStone];
}
}
– (void)didSimulatePhysics
{
SKNode *stone = [self childNodeWithName:@”stone”];
if (stone.position.y > CGRectGetMaxY(self.frame) + 50) {
[stone removeFromParent];
[self createStone];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[CurlingScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end