四角形の角を接続してブラブラさせるiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface ConnectScene : SKScene
@property (nonatomic, weak) SKNode *selected;
@end
@implementation ConnectScene
– (void)didMoveToView:(SKView *)view
{
[self createWall];
[self createPanels];
}
– (void)createWall
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
}
– (void)createPanels
{
SKColor *color = [SKColor orangeColor];
SKNode *buf;
float s = 50;
for (int i=0; i<4; i++) {
SKSpriteNode *panel = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(s,s)];
panel.name = @”panel”;
panel.position = CGPointMake(CGRectGetMidX(self.frame), s * i + 100);
[self addChild:panel];
panel.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:panel.size];
panel.physicsBody.categoryBitMask = 0x2;
panel.physicsBody.collisionBitMask = 0x2;
if (buf) {
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(10, 20)];
bar.position = CGPointMake(panel.position.x + s/2.2, panel.position.y – s/2.0);
bar.zPosition = 2;
[self addChild:bar];
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
bar.physicsBody.categoryBitMask = 0x5;
bar.physicsBody.collisionBitMask = 0x5;
for (int i=0; i<2; i++) {
SKPhysicsJointPin *pin;
if (i == 0) {
pin = [SKPhysicsJointPin jointWithBodyA:panel.physicsBody bodyB:bar.physicsBody anchor:CGPointMake(panel.position.x + s/2.2, panel.position.y – s/2.2)];
} else {
pin = [SKPhysicsJointPin jointWithBodyA:buf.physicsBody bodyB:bar.physicsBody anchor:CGPointMake(buf.position.x + s/2.2, buf.position.y + s/2.2)];
}
[self.physicsWorld addJoint:pin];
}
}
buf = panel;
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self enumerateChildNodesWithName:@”panel” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
SKSpriteNode *touchNode = [SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(10, 10)];
touchNode.position = p;
[self addChild:touchNode];
touchNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];
touchNode.physicsBody.dynamic = NO;
SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:touchNode.physicsBody bodyB:node.physicsBody anchor:touchNode.position];
[self.physicsWorld addJoint:pin];
self.selected = touchNode;
}
}];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
self.selected.position = p;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.selected removeFromParent];
self.selected = nil;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[ConnectScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end