
おもちゃの車で積み上げたタイヤに突っこめ!というiPhoneアプリのサンプルコードを描いてみます。
@import SpriteKit;
#import “ViewController.h”
@interface TireScene : SKScene
@property (nonatomic) CGPoint o;
@property (nonatomic, weak) SKNode *selected;
@property (nonatomic, weak) SKPhysicsJointSpring *spring;
@property (nonatomic, weak) SKShapeNode *line;
@end
@implementation TireScene
– (void)didMoveToView:(SKView *)view
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.backgroundColor = [SKColor colorWithRed:0.9 green:0.9 blue:1.0 alpha:1];
[self createTire];
[self createCar];
SKShapeNode *line = [SKShapeNode node];
[self addChild:line];
self.line = line;
self.line.fillColor = [SKColor clearColor];
self.line.strokeColor = [SKColor greenColor];
self.line.lineWidth = 3;
}
– (void)createTire
{
CGRect rect = CGRectMake(-30, –12, 60, 24);
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:5];
for (int i=0; i<8; i++) {
SKShapeNode *node = [SKShapeNode node];
node.position = CGPointMake(CGRectGetMaxX(self.frame) – 80, 32 * i + 16);
node.path = path.CGPath;
node.fillColor = [SKColor blackColor];
[self addChild:node];
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rect.size];
node.physicsBody.friction = 1.0;
node.physicsBody.restitution = 0.9;
}
}
– (void)createCar
{
SKSpriteNode *car = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(60, 36)];
car.name = @”car”;
car.position = CGPointMake(90, 30);
[self addChild:car];
car.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:car.size];
car.physicsBody.allowsRotation = NO;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:10 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
for (int i=0; i<2; i++) {
SKShapeNode *tire = [SKShapeNode node];
tire.path = path.CGPath;
tire.fillColor = [SKColor lightGrayColor];
tire.strokeColor = [SKColor darkGrayColor];
tire.lineWidth = 5;
tire.position = CGPointMake(i*40 – 20 + car.position.x, –15 + car.position.y);
[self addChild:tire];
tire.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:car.physicsBody bodyB:tire.physicsBody anchor:tire.position];
[self.physicsWorld addJoint:pin];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *node = [self nodeAtPoint:p];
if ([node.name isEqual:@”car”]) {
self.o = node.position;
self.selected = node;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
if (p.x > 40 && self.selected) {
self.selected.position = p;
UIBezierPath *path = [UIBezierPath bezierPath];
[path appendPath:[UIBezierPath bezierPathWithArcCenter:self.o radius:3 startAngle:0 endAngle:2.0*M_PI clockwise:NO]];
[path moveToPoint:self.o];
[path addLineToPoint:p];
self.line.path = path.CGPath;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.selected)
return;
CGPoint p = [[touches anyObject] locationInNode:self];
float d = self.o.x – p.x;
[self.selected.physicsBody applyImpulse:CGVectorMake(d * 3, 0)];
self.selected = nil;
self.line.path = nil;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[TireScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end