ボールの重りをずらして、コロコロころがすiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface RollScene : SKScene
@property (nonatomic, weak) SKNode *select;
@end
@implementation RollScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [SKColor colorWithWhite:0.95 alpha:1];
[self createSlope];
[self createCustomBox];
[self createBall];
[self createDropButton];
[self showTitle];
}
– (void)createSlope
{
SKSpriteNode *slope = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(600, 5)];
slope.position = CGPointMake(CGRectGetMidX(self.frame), 50);
slope.zRotation = –M_PI * 0.02;
[self addChild:slope];
slope.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:slope.size];
slope.physicsBody.dynamic = NO;
}
– (void)createCustomBox
{
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(-40, –40, 80, 80) cornerRadius:5];
SKShapeNode *box = [SKShapeNode node];
box.path = path.CGPath;
box.position = CGPointMake(50, CGRectGetMaxY(self.view.frame) – 70);
box.fillColor = [SKColor clearColor];
box.strokeColor = [SKColor blackColor];
box.lineWidth = 2;
[self addChild:box];
}
– (void)createBall
{
UIBezierPath *pathA = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:30 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *ball = [SKShapeNode node];
ball.name = @”ball”;
ball.position = CGPointMake(50, CGRectGetMaxY(self.view.frame) – 70);
ball.path = pathA.CGPath;
ball.fillColor = [SKColor lightGrayColor];
[self addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
ball.physicsBody.density = 0.1;
ball.physicsBody.dynamic = NO;
SKSpriteNode *weight = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(15, 15)];
weight.name = @”weight”;
weight.zRotation = M_PI * 0.25;
[ball addChild:weight];
}
– (void)createDropButton
{
SKLabelNode *l = [SKLabelNode node];
l.name = @”drop button”;
l.position = CGPointMake(50, 160);
l.text = @”↓”;
l.fontSize = 50;
l.fontColor = [SKColor lightGrayColor];
[self addChild:l];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *ball = [self childNodeWithName:@”ball”];
if ([ball containsPoint:p]) {
self.select = ball;
}
SKNode *button = [self childNodeWithName:@”drop button”];
if ([button containsPoint:p]) {
SKSpriteNode *weight = (SKSpriteNode *)[ball childNodeWithName:@”weight”];
weight.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:weight.size];
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:ball.physicsBody bodyB:weight.physicsBody anchor:[ball convertPoint:weight.position toNode:self]];
[self.physicsWorld addJoint:pin];
ball.physicsBody.dynamic = YES;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.select) {
CGPoint p = [[touches anyObject] locationInNode:self.select];
SKNode *weight = [self.select childNodeWithName:@”weight”];
weight.position = p;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.select = nil;
}
– (void)showTitle
{
SKLabelNode *title = [SKLabelNode node];
title.text = @”corocoro”;
title.fontColor = [UIColor colorWithWhite:0.8 alpha:1.0];
title.fontSize = 100;
title.position = CGPointMake(260, 160);
title.zRotation = M_PI * 0.1;
[self addChild:title];
}
– (void)didSimulatePhysics
{
SKNode *ball = [self childNodeWithName:@”ball”];
if (ball.position.y < 0) {
[ball removeFromParent];
[self createBall];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[RollScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end