上から降ってくるレンガで、0から9までをカウントするタイマー。といった感じでiPhoneアプリのサンプルコードを描いてみます。
今回使った画像
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
const int one[] = {
0,0,0,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,1,0,0,
0,0,0,0,0
};
const int two[] = {
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
0,1,1,1,0,
0,1,0,0,0,
0,1,1,1,0,
0,0,0,0,0
};
const int three[] = {
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
0,1,1,1,0,
0,0,0,1,0,
0,1,1,1,0,
0,0,0,0,0
};
const int four[] = {
0,0,0,0,0,
0,1,0,1,0,
0,1,0,1,0,
0,1,1,1,0,
0,0,0,1,0,
0,0,0,1,0,
0,0,0,0,0
};
const int five[] = {
0,0,0,0,0,
0,1,1,1,0,
0,1,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
0,1,1,1,0,
0,0,0,0,0
};
const int six[] = {
0,0,0,0,0,
0,1,1,1,0,
0,1,0,0,0,
0,1,1,1,0,
0,1,0,1,0,
0,1,1,1,0,
0,0,0,0,0
};
const int seven[] = {
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
0,0,0,1,0,
0,0,0,1,0,
0,0,0,1,0,
0,0,0,0,0
};
const int eight[] = {
0,0,0,0,0,
0,1,1,1,0,
0,1,0,1,0,
0,1,1,1,0,
0,1,0,1,0,
0,1,1,1,0,
0,0,0,0,0
};
const int nine[] = {
0,0,0,0,0,
0,1,1,1,0,
0,1,0,1,0,
0,1,1,1,0,
0,0,0,1,0,
0,1,1,1,0,
0,0,0,0,0
};
const int zero[] = {
0,0,0,0,0,
0,1,1,1,0,
0,1,0,1,0,
0,1,0,1,0,
0,1,0,1,0,
0,1,1,1,0,
0,0,0,0,0
};
@interface BrickTimer : SKScene
@property BOOL contentCreated;
@property int counter;
@property int timer;
@property (nonatomic, strong) NSArray *numbers;
@end
@implementation BrickTimer
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
SKSpriteNode *ground = [[SKSpriteNode alloc] initWithColor:[SKColor brownColor] size:CGSizeMake(self.frame.size.width, 20)];
ground.position = CGPointMake(CGRectGetMidX(self.frame), 10);
[self addChild:ground];
ground.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ground.size];
ground.physicsBody.dynamic = NO;
ground.physicsBody.restitution = 0;
}
– (void)update:(NSTimeInterval)currentTime
{
if (self.counter < 35) {
float x = (self.counter % 5);
float y = (self.counter / 5);
int* colorMap = [self.numbers[self.timer] pointerValue];
int colorIndex = (30 – y * 5) + x;
SKTexture *texture = colorMap[colorIndex] ? [SKTexture textureWithImageNamed:@”brickA”] : [SKTexture textureWithImageNamed:@”brickB”];
SKSpriteNode *brick = [SKSpriteNode spriteNodeWithTexture:texture size:CGSizeMake(60, 62)];
brick.name = [@(self.counter) stringValue];
brick.position = CGPointMake(x * 60 + 40, y * 60 + CGRectGetMaxY(self.frame));
[self addChild:brick];
brick.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(59, 60)];
brick.physicsBody.restitution = 0;
brick.physicsBody.allowsRotation = NO;
} else if (self.counter < 120) {
int index = self.counter – 85;
SKNode *node = [self childNodeWithName:[@(index) stringValue]];
node.name = @”fall”;
node.physicsBody.collisionBitMask = 0x0;
} else if (self.counter > 150) {
self.timer = (self.timer + 1) % 10;
self.counter = –1;
}
self.counter++;
}
– (void)didSimulatePhysics
{
[self enumerateChildNodesWithName:@”fall” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.y < 0) {
[node removeFromParent];
}
}];
}
– (NSArray *)numbers
{
if (!_numbers) {
_numbers = @[[NSValue valueWithPointer:zero],
[NSValue valueWithPointer:one],
[NSValue valueWithPointer:two],
[NSValue valueWithPointer:three],
[NSValue valueWithPointer:four],
[NSValue valueWithPointer:five],
[NSValue valueWithPointer:six],
[NSValue valueWithPointer:seven],
[NSValue valueWithPointer:eight],
[NSValue valueWithPointer:nine],
];
}
return _numbers;
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[BrickTimer alloc] initWithSize:self.view.bounds.size];
[spriteView presentScene:scene];
}
@end