三角を上下にコントロールして、壁をよけるゲームiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface FlyScene : SKScene
@property NSUInteger counter;
@property BOOL touch;
@end
@implementation FlyScene
– (void)didMoveToView:(SKView *)view
{
self.backgroundColor = [self color:3];
self.physicsWorld.gravity = CGVectorMake(0, –0.05);
[self createFlyTriangle];
[self createUpButton];
}
– (void)createFlyTriangle
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(-30, –20)];
[path addLineToPoint:CGPointMake(30, 0)];
[path addLineToPoint:CGPointMake(-30, 20)];
[path closePath];
SKShapeNode *tri = [SKShapeNode node];
tri.name = @”tri”;
tri.path = path.CGPath;
tri.fillColor = [self color:2];
[self addChild:tri];
tri.position = CGPointMake(100, CGRectGetMidY(self.frame));
tri.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];
tri.physicsBody.density = 0.1;
}
– (void)createUpButton
{
SKLabelNode *l = [SKLabelNode node];
l.name = @”up”;
l.text = @”up”;
l.fontSize = 50;
l.position = CGPointMake(CGRectGetMaxX(self.frame) – 60, 40);
l.fontColor = [self color:4];
[self addChild:l];
}
– (void)createBarrier:(CGPoint)p
{
SKSpriteNode *barrier = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(20, 60)];
barrier.name = @”barrier”;
barrier.position = p;
[self addChild:barrier];
barrier.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:barrier.size];
barrier.physicsBody.linearDamping = 1.0;
barrier.physicsBody.density = 100;
barrier.physicsBody.affectedByGravity = NO;
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *upBtn = [self childNodeWithName:@”up”];
if ([upBtn containsPoint:p]) {
self.touch = YES;
}
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *upBtn = [self childNodeWithName:@”up”];
if (![upBtn containsPoint:p]) {
self.touch = NO;
}
}
– (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touch = NO;
}
const int points[] = {1,0,1,2,2,0,0,3,0,0,1,0,0,0,0,1};
– (void)update:(NSTimeInterval)currentTime
{
if (self.counter % 300 == 0) {
NSUInteger idx = (self.counter / 300) % (sizeof(points)/sizeof(int));
[self createBarrier:CGPointMake(CGRectGetMaxX(self.frame), (points[idx]+1) * 100)];
}
self.counter++;
[self enumerateChildNodesWithName:@”barrier” usingBlock:^(SKNode *node, BOOL *stop) {
node.physicsBody.velocity = CGVectorMake(-30, 0);
if (node.position.x < –20) {
[node removeFromParent];
}
}];
SKNode *tri = [self childNodeWithName:@”tri”];
if (self.touch) {
[tri.physicsBody applyForce:CGVectorMake(0, 1)];
}
if (tri.position.y < 20) {
tri.physicsBody.velocity = CGVectorMake(0, 0);
tri.position = CGPointMake(tri.position.x, 20);
} else if (tri.position.y > 300) {
tri.physicsBody.velocity = CGVectorMake(0, 0);
tri.position = CGPointMake(tri.position.x, 300);
}
}
#define ColorHex(rgbValue) [SKColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
– (SKColor *)color:(NSInteger)i
{
switch (i) {
case 0:
return ColorHex(0x2C3E50);
case 1:
return ColorHex(0xFC4349);
case 2:
return ColorHex(0xD7DADB);
case 3:
return ColorHex(0x6DBCDB);
case 4:
return ColorHex(0xF2F2F2);
default:
break;
}
return nil;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[FlyScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end