
同じ模様の箱をぺたぺたとつなげて遊ぶiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface LinkBoxScene : SKScene <SKPhysicsContactDelegate>
@property (nonatomic, weak) SKNode *select;
@end
@implementation LinkBoxScene
– (void)didMoveToView:(SKView *)view
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.contactDelegate = self;
self.backgroundColor = [SKColor greenColor];
[self createBoxes];
}
– (void)createBoxes
{
NSArray *marks = @[@”○”, @”△”, @”□”];
for (int i=0; i<3; i++) {
SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(40, 40)];
box.name = marks[i];
box.position = CGPointMake(80, i * 100 + 160);
[self addChild:box];
box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];
box.physicsBody.dynamic = NO;
box.physicsBody.contactTestBitMask = 0x2;
SKLabelNode *l = [SKLabelNode node];
l.text = box.name;
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
[box addChild:l];
}
for (int i=0; i<10; i++) {
SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(40, 40)];
box.name = marks[i % 3];
box.position = CGPointMake((i % 5) * 40 + 80, (i/5) * 50 + 60);
[self addChild:box];
box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];
box.physicsBody.categoryBitMask = 0x2;
SKLabelNode *l = [SKLabelNode node];
l.text = box.name;
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
[box addChild:l];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *marks = [NSSet setWithObjects:@”○”, @”△”, @”□”, nil];
CGPoint p = [[touches anyObject] locationInNode:self];
NSArray *hits = [self nodesAtPoint:p];
if (hits.count > 0) {
SKNode *hit = hits[0];
if ([marks containsObject:hit.name]) {
self.select = hit;
self.select.physicsBody.affectedByGravity = NO;
}
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self.select runAction:[SKAction moveTo:p duration:0.1]];
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.select.physicsBody.affectedByGravity = YES;
self.select = nil;
}
– (void)didBeginContact:(SKPhysicsContact *)contact
{
[self touchesEnded:nil withEvent:nil];
if([contact.bodyA.node.name isEqual:contact.bodyB.node.name]) {
SKSpriteNode *node = contact.bodyB.categoryBitMask == 0x2 ? (SKSpriteNode *)contact.bodyB.node : (SKSpriteNode *)contact.bodyA.node;
[node removeAllActions];
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:node.size];
node.physicsBody.dynamic = NO;
node.physicsBody.contactTestBitMask = 0x2;
node.physicsBody.categoryBitMask = 0x1;
[node setColor:[SKColor grayColor]];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[LinkBoxScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end