フェンシングっぽいiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface FencingScene : SKScene
@end
@implementation FencingScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor colorWithRed:0.8 green:1.0 blue:0.8 alpha:1.0];
[self createPlayer];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(createFallBrick) userInfo:nil repeats:YES];
}
– (void)createPlayer
{
UIBezierPath *headPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)];
SKShapeNode *head = [SKShapeNode node];
head.path = headPath.CGPath;
head.fillColor = [SKColor darkGrayColor];
head.lineWidth = 0;
head.position = CGPointMake(60, 180);
[self addChild:head];
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(35, 55)];
body.position = CGPointMake(60, 130);
[self addChild:body];
body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:body.size];
body.physicsBody.dynamic = NO;
body.physicsBody.categoryBitMask = 0x1;
SKSpriteNode *lHand = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(40, 15)];
lHand.position = CGPointMake(30, 150);
[self addChild:lHand];
SKSpriteNode *lFoot = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(50, 15)];
lFoot.position = CGPointMake(30, 90);
lFoot.zRotation = M_PI_4;
[self addChild:lFoot];
SKSpriteNode *rFootU = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(30, 15)];
rFootU.position = CGPointMake(90, 110);
[self addChild:rFootU];
SKSpriteNode *rFootB = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(15, 40)];
rFootB.position = CGPointMake(100, 95);
[self addChild:rFootB];
SKSpriteNode *rHandA = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(30, 15)];
rHandA.position = CGPointMake(90, 150);
[self addChild:rHandA];
rHandA.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rHandA.size];
rHandA.physicsBody.collisionBitMask = 0x3;
SKSpriteNode *rHandB = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(30, 15)];
rHandB.zPosition = 1;
rHandB.position = CGPointMake(120, 150);
[self addChild:rHandB];
rHandB.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rHandB.size];
rHandB.physicsBody.collisionBitMask = 0x3;
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(80, 5)];
bar.name = @”bar”;
bar.position = CGPointMake(160, 150);
[self addChild:bar];
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
bar.physicsBody.collisionBitMask = 0x3;
SKPhysicsJointPin *pinA = [SKPhysicsJointPin jointWithBodyA:body.physicsBody bodyB:rHandA.physicsBody anchor:CGPointMake(80, 150)];
[self.physicsWorld addJoint:pinA];
SKPhysicsJointPin *pinB = [SKPhysicsJointPin jointWithBodyA:rHandA.physicsBody bodyB:rHandB.physicsBody anchor:CGPointMake(110, 150)];
[self.physicsWorld addJoint:pinB];
SKPhysicsJointPin *pinC = [SKPhysicsJointPin jointWithBodyA:rHandB.physicsBody bodyB:bar.physicsBody anchor:CGPointMake(135, 150)];
[self.physicsWorld addJoint:pinC];
SKPhysicsJointSliding *sliding = [SKPhysicsJointSliding jointWithBodyA:body.physicsBody bodyB:bar.physicsBody anchor:CGPointMake(140, 150) axis:CGVectorMake(1, 0)];
[self.physicsWorld addJoint:sliding];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *bar = [self childNodeWithName:@”bar”];
[bar.physicsBody applyImpulse:CGVectorMake(6, 0)];
}
– (void)createFallBrick
{
float hue = (arc4random() % 8) * 0.1;
SKSpriteNode *brick = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:hue saturation:0.9 brightness:0.8 alpha:1.0] size:CGSizeMake(20, 20)];
brick.name = @”brick”;
brick.zRotation = M_PI_4;
brick.position = CGPointMake(hue * 10 + 190, CGRectGetMaxY(self.frame));
[self addChild:brick];
brick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:brick.size];
}
– (void)update:(NSTimeInterval)currentTime
{
[self enumerateChildNodesWithName:@”brick” usingBlock:^(SKNode *node, BOOL *stop) {
node.physicsBody.velocity = CGVectorMake(node.physicsBody.velocity.dx, MAX(-30, node.physicsBody.velocity.dy));
}];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[FencingScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end