
風船をタップして、荷物をゴール地点に落とすiPhoneゲームのサンプルコードを描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface BalloonScene : SKScene
@property BOOL contentCreated;
@end
@implementation BalloonScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
[self createBalloonBox];
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(80, 20)];
ground.position = CGPointMake(100, 50);
[self addChild:ground];
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
}
– (void)createBalloonBox
{
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-20, –20, 40, 40)];
SKShapeNode *balloon = [SKShapeNode node];
balloon.name = @”balloon”;
balloon.path = path.CGPath;
balloon.position = CGPointMake(350, 350);
balloon.fillColor = [SKColor redColor];
[self addChild:balloon];
balloon.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(40, 40)];
box.name = @”box”;
box.position = CGPointMake(balloon.position.x, balloon.position.y – 100);
[self addChild:box];
box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];
SKPhysicsJointLimit *j = [SKPhysicsJointLimit jointWithBodyA:balloon.physicsBody bodyB:box.physicsBody anchorA:CGPointMake(balloon.position.x, balloon.position.y) anchorB:CGPointMake(box.position.x, box.position.y)];
j.maxLength = 100;
[self.physicsWorld addJoint:j];
SKShapeNode *rope = [SKShapeNode node];
rope.name = @”rope”;
[self addChild:rope];
}
– (void)update:(NSTimeInterval)currentTime
{
SKNode *balloon = [self childNodeWithName:@”balloon”];
balloon.physicsBody.velocity = CGVectorMake(-40, 50);
}
– (void)didSimulatePhysics
{
SKNode *balloon = [self childNodeWithName:@”balloon”];
SKNode *box = [self childNodeWithName:@”box”];
SKShapeNode *rope = (SKShapeNode*)[self childNodeWithName:@”rope”];
if (box.position.x > –50) {
if (rope) {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:box.position];
[path addLineToPoint:CGPointMake(balloon.position.x, balloon.position.y – 20)];
rope.path = path.CGPath;
}
} else {
[balloon removeFromParent];
[box removeFromParent];
[rope removeFromParent];
[self createBalloonBox];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *balloon = [self childNodeWithName:@”balloon”];
SKNode *rope = [self childNodeWithName:@”rope”];
SKNode *box = [self childNodeWithName:@”box”];
if ([balloon containsPoint:p]) {
[balloon removeFromParent];
[rope removeFromParent];
box.name = @”boxUsed”;
[self createBalloonBox];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[BalloonScene alloc] initWithSize:spriteView.bounds.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end