
回転板をかいくぐって一番にゴールするのはどれだ?という感じでiPhoneアプリのサンプルコードを描いてみます。
@import SpriteKit;
#import “ViewController.h”
@interface SpinScene : SKScene
@property (nonatomic) BOOL move;
@end
@implementation SpinScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [UIColor orangeColor];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity = CGVectorMake(0, 0);
[self createSpinTable];
[self createGoalLine];
[self createPlayer];
}
– (void)createSpinTable
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:150 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *t = [SKShapeNode node];
t.name = @”table”;
t.path = path.CGPath;
t.lineWidth = 0;
t.fillColor = [SKColor greenColor];
t.position = CGPointMake(CGRectGetMidX(self.frame), 250);
[self addChild:t];
t.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:150];
t.physicsBody.categoryBitMask = 0x1 << 1;
t.physicsBody.collisionBitMask = 0x1 << 1;
for (int i=0; i<8; i++) {
SKSpriteNode *l = [SKSpriteNode spriteNodeWithColor:[[SKColor whiteColor] colorWithAlphaComponent:0.5] size:CGSizeMake(280, 3)];
l.zRotation = i * M_PI / 4.0;
[t addChild:l];
}
SKPhysicsJointPin *pinA = [SKPhysicsJointPin jointWithBodyA:self.physicsBody bodyB:t.physicsBody anchor:t.position];
[self.physicsWorld addJoint:pinA];
float dangle = M_PI / 2.5;
for (int i=0; i<5; i++) {
SKSpriteNode *spinbar = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(30, 3)];
spinbar.name = @”bar”;
float r = i == 0 ? 0 : (arc4random() % 50) + 80;
float x = r * cos(dangle * i) + t.position.x;
float y = r * sin(dangle * i) + t.position.y;
spinbar.position = CGPointMake(x, y);
[self addChild:spinbar];
spinbar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spinbar.size];
spinbar.physicsBody.categoryBitMask = 0x1 << 2;
spinbar.physicsBody.collisionBitMask = 0x1 << 2;
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:t.physicsBody bodyB:spinbar.physicsBody anchor:spinbar.position];
[self.physicsWorld addJoint:pin];
}
}
– (void)createGoalLine
{
SKSpriteNode *line = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 20)];
line.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 80);
[self addChild:line];
}
– (void)createPlayer
{
for (int i=0; i<3; i++) {
SKSpriteNode *player = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:0.3 * i saturation:0.6 brightness:1 alpha:1] size:CGSizeMake(30, 30)];
player.name = @”player”;
player.position = CGPointMake(CGRectGetMidX(self.frame) – 50 + 50 * i, 50);
[self addChild:player];
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.size];
player.physicsBody.allowsRotation = NO;
player.physicsBody.categoryBitMask = 0x1 << 3;
player.physicsBody.collisionBitMask = 0x1 << 2 | 0x1 << 3;
SKLabelNode *eye = [SKLabelNode node];
eye.text = @”x x”;
eye.fontSize = 20;
eye.fontColor = [SKColor darkGrayColor];
[player addChild:eye];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.move = YES;
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.move = NO;
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *table = [self childNodeWithName:@”table”];
table.physicsBody.angularVelocity = 1;
[self enumerateChildNodesWithName:@”bar” usingBlock:^(SKNode *node, BOOL *stop) {
node.physicsBody.angularVelocity = 6;
}];
[self enumerateChildNodesWithName:@”player” usingBlock:^(SKNode *player, BOOL *stop) {
if (self.move) {
player.physicsBody.velocity = CGVectorMake(0, 40);
} else {
player.physicsBody.velocity = CGVectorMake(0, 0);
}
}];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[SpinScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end