
道のロックを外して、自由に動き回れるようにするiPhoneアプリのサンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface DoorScene : SKScene
@property (nonatomic, weak) SKNode *select;
@end
@implementation DoorScene
– (void)didMoveToView:(SKView *)view
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
[self createWall];
[self createLock];
[self createBall];
[self createKey];
[self createButton];
}
– (void)createWall
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.dynamic = NO;
for (int i=0; i<2; i++) {
float w = 300 – i * 150;
float h = 300 – i * 150;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(-w/2.0, -h/2.0, w, h)];
SKShapeNode *rect = [SKShapeNode node];
rect.path = path.CGPath;
rect.position = CGPointMake(160, 250);
rect.strokeColor = [SKColor lightGrayColor];
rect.lineWidth = 4;
[self addChild:rect];
rect.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:path.CGPath];
rect.physicsBody.dynamic = NO;
}
for (int i=0; i<4; i++) {
float angle = i * M_PI / 2.0 + M_PI/4.0;
float x = 180 * cos(angle) + 160;
float y = 180 * sin(angle) + 250;
SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(50, 4)];
bar.position = CGPointMake(x, y);
bar.zRotation = angle + M_PI / 2.0;
[self addChild:bar];
bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];
bar.physicsBody.dynamic = NO;
}
}
– (void)createLock
{
for (int i=0; i<2; i++) {
SKSpriteNode *lock = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(40, 40)];
lock.name = @”lock”;
[lock addChild:[self hole]];
lock.position = CGPointMake(160, 140 + i * 220);
[self addChild:lock];
lock.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:lock.size];
lock.physicsBody.dynamic = NO;
}
}
– (void)createKey
{
for (int i=0; i<2; i++) {
SKShapeNode *key = [self hole];
key.name = @”key”;
key.position = CGPointMake(120 * i + 100, 50);
key.fillColor = [SKColor yellowColor];
[self addChild:key];
}
}
– (SKShapeNode *)hole
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 7) radius:10 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
[path moveToPoint:CGPointMake(-8, –15)];
[path addLineToPoint:CGPointMake(8, –15)];
[path addLineToPoint:CGPointMake(5, –2)];
[path addLineToPoint:CGPointMake(-5, –2)];
[path closePath];
SKShapeNode *hole = [SKShapeNode node];
hole.fillColor = [SKColor darkGrayColor];
hole.path = path.CGPath;
return hole;
}
– (void)createBall
{
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:20 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *ball = [SKShapeNode node];
ball.name = @”ball”;
ball.path = path.CGPath;
ball.position = CGPointMake(50, 250);
ball.fillColor = [SKColor purpleColor];
[self addChild:ball];
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
ball.physicsBody.restitution = 1.0;
ball.physicsBody.friction = 0;
[ball.physicsBody applyImpulse:CGVectorMake(0, 10)];
}
– (void)createButton
{
SKLabelNode *l = [SKLabelNode node];
l.name = @”button”;
l.text = @”Go”;
l.position = CGPointMake(160, 250);
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
[self addChild:l];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *ball = [self childNodeWithName:@”ball”];
if (abs(ball.physicsBody.velocity.dx) < 1 && abs(ball.physicsBody.velocity.dy) < 1) {
[ball removeFromParent];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *button = [self childNodeWithName:@”button”];
CGPoint p = [[touches anyObject] locationInNode:self];
if ([button containsPoint:p]) {
[self createBall];
}
[self enumerateChildNodesWithName:@”key” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
self.select = node;
}
}];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
self.select.position = p;
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (self.select) {
[self enumerateChildNodesWithName:@”lock” usingBlock:^(SKNode *node, BOOL *stop) {
if([node containsPoint:self.select.position]) {
[node removeFromParent];
[self.select removeFromParent];
}
}];
}
self.select = 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 = [[DoorScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end