バンジーでビヨンビヨンするiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface BungyScene : SKScene
@end
@implementation BungyScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor colorWithRed:0.9 green:0.9 blue:1 alpha:1];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[self createSpringboard];
[self createJumper];
[self createButton];
}
– (void)createSpringboard
{
SKSpriteNode *board = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(120, 10)];
board.name = @”board”;
board.position = CGPointMake(CGRectGetMaxX(self.frame)-60, CGRectGetMaxY(self.frame) –120);
[self addChild:board];
board.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:board.size];
board.physicsBody.dynamic = NO;
}
– (void)createJumper
{
SKNode *jumper = [SKNode node];
jumper.name = @”jumper”;
jumper.position = CGPointMake(CGRectGetMaxX(self.frame)-110, CGRectGetMaxY(self.frame) –80);
[self addChild:jumper];
SKColor *color = [SKColor blackColor];
SKShapeNode *head = [SKShapeNode node];
head.path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO].CGPath;
head.fillColor = [SKColor blackColor];
head.position = CGPointMake(0, 30);
[jumper addChild:head];
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(15, 50)];
body.position = CGPointMake(0, –10);
[jumper addChild:body];
for (int i=0; i<2; i++) {
SKSpriteNode *hand = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(30, 10)];
hand.position = CGPointMake(i ? 15 : –15, 20);
hand.zRotation = i ? 0.2 : –0.2;
[body addChild:hand];
}
SKSpriteNode *mark = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(10, 10)];
mark.position = CGPointMake(5, –15);
[body addChild:mark];
jumper.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(30, 70)];
SKNode *board = [self childNodeWithName:@”board”];
SKSpriteNode *anchor = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(5, 5)];
anchor.position = CGPointMake(board.position.x – 50, board.position.y);
[self addChild:anchor];
anchor.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:anchor.size];
anchor.physicsBody.dynamic = NO;
SKNode *n0;
SKNode *n1;
for (int i=0; i<10; i++) {
if (i == 0) n0 = anchor;
if (i == 9) n1 = jumper;
else {
n1 = [SKSpriteNode spriteNodeWithColor:[UIColor brownColor] size:CGSizeMake(5, 5)];
n1.position = anchor.position;
[self addChild:n1];
n1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:2];
}
CGPoint p = (i != 9) ? n1.position : [jumper convertPoint:mark.position toNode:self];
SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:n0.physicsBody bodyB:n1.physicsBody anchorA:n0.position anchorB:p];
spring.damping = 1;
spring.frequency = 35;
[self.physicsWorld addJoint:spring];
n0 = n1;
}
}
– (void)createButton
{
SKLabelNode *btn = [SKLabelNode node];
btn.name = @”button”;
btn.fontSize = 50;
btn.text = @”JUMP”;
btn.fontColor = [SKColor greenColor];
btn.position = CGPointMake(CGRectGetMidX(self.frame), 30);
[self addChild:btn];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *btn = [self childNodeWithName:@”button”];
if ([btn containsPoint:p]) {
SKNode *jumper = [self childNodeWithName:@”jumper”];
[jumper.physicsBody applyImpulse:CGVectorMake(-15, 25)];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[BungyScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end