黄色い四角をジャンプで上まで運んでいくシンプルなiPhoneゲームのサンプルコードを描いてみます。
サンプルを動画でみる
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface JumpScene : SKScene
@property BOOL contentCreated;
@end
@implementation JumpScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.physicsWorld.gravity = CGVectorMake(0, –3.0);
self.backgroundColor = [UIColor lightGrayColor];
SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(320, 10)];
ground.position = CGPointMake(160, 10);
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
[self addChild:ground];
[self createMoveFloor];
[self createJumper];
}
– (void)createMoveFloor
{
for (int i=0; i<4; i++) {
SKSpriteNode *floor = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(80, 10)];
floor.name = @”floor”;
float y = 100 * i + 100;
floor.userData = [@{@”vx”:@(50 + 10 * i),
@”y” : @(y)} mutableCopy];
floor.position = CGPointMake(50 + arc4random() % 200, y);
[self addChild:floor];
floor.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:floor.size];
floor.physicsBody.velocity = CGVectorMake((i + 5) / 5.0, 0);
floor.physicsBody.affectedByGravity = NO;
floor.physicsBody.allowsRotation = NO;
}
}
– (void)createJumper
{
SKSpriteNode *jumper = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(40, 40)];
jumper.name = @”jumper”;
jumper.position = CGPointMake(160, 20);
[self addChild:jumper];
jumper.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:jumper.size];
jumper.physicsBody.friction = 1.0;
jumper.physicsBody.linearDamping = 1.0;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKNode *jumper = [self childNodeWithName:@”jumper”];
[jumper.physicsBody applyImpulse:CGVectorMake(0, 35)];
}
– (void)update:(NSTimeInterval)currentTime
{
[self enumerateChildNodesWithName:@”floor” usingBlock:^(SKNode *node, BOOL *stop) {
float dx = [[node.userData valueForKey:@”vx”] floatValue];
if (node.position.x < 30) {
dx = fabs(dx);
[node.userData setValue:@(dx) forKey:@”vx”];
} else if (node.position.x > 280) {
dx = –fabs(dx);
[node.userData setValue:@(dx) forKey:@”vx”];
}
node.physicsBody.velocity = CGVectorMake(dx, 0);
node.position = CGPointMake(node.position.x, [[node.userData valueForKey:@”y”] floatValue]);
}];
}
– (void)didSimulatePhysics
{
SKNode *node = [self childNodeWithName:@”jumper”];
if (!CGRectContainsPoint(self.frame, node.position)) {
[node removeFromParent];
[self createJumper];
}
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
}
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[JumpScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end