
壊れそうなブロックのところを突き破って進んでいく縦スクロールiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface CrashSCcene : SKScene
@property (nonatomic, strong) NSMutableArray *map;
@property (nonatomic) CGPoint touchPoint;
@end
@implementation CrashSCcene
– (void)didMoveToView:(SKView *)view
{
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.backgroundColor = [SKColor colorWithRed:1 green:0.9 blue:0.85 alpha:1];
[self initMap];
[self createPlayer];
}
– (void)initMap
{
self.map = [NSMutableArray array];
for (int i=0; i<2; i++) {
SKNode *map = [self createMapWithBlock];
map.position = CGPointMake(0, i * CGRectGetMaxY(self.frame));
[self.map addObject:map];
}
}
– (SKNode *)createMapWithBlock
{
SKNode *map = [SKNode node];
[self addChild:map];
for (int i=0; i<2; i++) {
float y = 200 + i * 150 + arc4random() % 50;
int num = arc4random() % 5;
for (int j=0; j<5; j++) {
SKNode *block = (j == num) ? [self createBlockB] : [self createBlockA];
block.position = CGPointMake(j * 60 + 40, y);
[map addChild:block];
}
}
return map;
}
-(SKNode *)createBlockA
{
SKColor *color = [SKColor colorWithHue:(arc4random()%10) * 0.1 saturation:0.8 brightness:0.8 alpha:1.0];
SKSpriteNode *block = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(55, 40)];
block.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:block.size];
block.physicsBody.dynamic = NO;
return block;
}
-(SKNode *)createBlockB
{
SKColor *color = [SKColor colorWithHue:(arc4random()%10) * 0.1 saturation:0.8 brightness:0.8 alpha:1.0];
SKNode *block = [SKNode node];
float dx = 55/3.0;
float dy = 40/3.0;
for (int i=0; i<9; i++) {
if (i%2) continue;
float x = (i%3) * dx – dx * 1;
float y = (i/3) * dy – dy * 1;
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(dx, dy)];
n.position = CGPointMake(x, y);
[block addChild:n];
n.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:n.size];
n.physicsBody.density = 0.1;
}
return block;
}
– (void)createPlayer
{
UIBezierPath *triangle = [UIBezierPath bezierPath];
[triangle moveToPoint:CGPointMake(-20, –20)];
[triangle addLineToPoint:CGPointMake(0, 20)];
[triangle addLineToPoint:CGPointMake(20, –20)];
[triangle closePath];
SKShapeNode *player = [SKShapeNode node];
player.name = @”player”;
player.path = triangle.CGPath;
player.fillColor = [SKColor blackColor];
player.strokeColor = [SKColor whiteColor];
player.position = CGPointMake(160, 50);
player.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:triangle.CGPath];
player.physicsBody.allowsRotation = NO;
[self addChild:player];
}
– (void)update:(NSTimeInterval)currentTime
{
for (int i = self.map.count – 1; i>=0; i–) {
SKNode *n = self.map[i];
n.position = CGPointMake(n.position.x, n.position.y – 1);
if (i == 0 && n.position.y < –CGRectGetMaxY(self.frame)) {
[self.map removeObject:n];
[n removeFromParent];
SKNode *next = [self createMapWithBlock];
next.position = CGPointMake(0, CGRectGetMaxY(self.frame));
[self.map addObject:next];
}
}
SKNode *player = [self childNodeWithName:@”player”];
if (!CGPointEqualToPoint(self.touchPoint, CGPointZero)) {
if (self.touchPoint.x < player.position.x) {
player.physicsBody.velocity = CGVectorMake(-100, 0);
} else if (self.touchPoint.x > player.position.x) {
player.physicsBody.velocity = CGVectorMake(100, 0);
}
}
}
– (void)didSimulatePhysics
{
SKNode *player = [self childNodeWithName:@”player”];
if (player.position.y < 20) {
SKAction *flash = [SKAction repeatAction:[SKAction sequence:@[[SKAction fadeOutWithDuration:0.2], [SKAction fadeInWithDuration:0.2]]] count:4];
[player runAction:[SKAction group:@[flash, [SKAction moveToY:60 duration:0.1]]]];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
self.touchPoint = p;
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
self.touchPoint = p;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.touchPoint = CGPointZero;
SKNode *player = [self childNodeWithName:@”player”];
player.physicsBody.velocity = CGVectorMake(0, 0);
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[CrashSCcene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end