ボックスをタップすると割れて十が出てくるiPhoneアプリのサンプルコード
#import “ViewController.h”
@import SpriteKit;
@interface BoxScene : SKScene
@end
@implementation BoxScene
– (void)didMoveToView:(SKView *)view
{
[self createBackGround];
}
– (void)createBackGround
{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(-20, 80)];
[path addLineToPoint:CGPointMake(20, 0)];
[path addLineToPoint:CGPointMake(-20, –80)];
[path addLineToPoint:CGPointMake(10, 0)];
for (int i=0; i<10; i++) {
SKShapeNode *n = [SKShapeNode shapeNodeWithPath:path.CGPath];
n.fillColor = [SKColor whiteColor];
n.position = CGPointMake(i * 80, CGRectGetMidY(self.frame));
[self addChild:n];
}
}
– (void)update:(NSTimeInterval)currentTime
{
static NSTimeInterval lastCreate = 0;
if (currentTime – lastCreate > 1) {
lastCreate = currentTime;
[self createBoxPlus];
}
static NSTimeInterval last = 0;
if (last == 0) {
last = currentTime;
}
float dx = 80 * (currentTime – last);
[self enumerateChildNodesWithName:@”box” usingBlock:^(SKNode *node, BOOL *stop) {
node.position = CGPointMake(node.position.x + dx, node.position.y);
if (node.position.x > CGRectGetMaxX(self.frame) + 50) {
[node removeFromParent];
}
}];
last = currentTime;
}
– (void)createBoxPlus
{
SKColor *color = [SKColor colorWithHue:(arc4random()%10) *0.1 saturation:0.9 brightness:1 alpha:1];
CGSize parts[] = {CGSizeMake(20, 40),CGSizeMake(10, 20), CGSizeMake(50, 10)};
SKNode *box = [SKNode node];
box.name = @”box”;
[self addChild:box];
box.position = CGPointMake(-20, CGRectGetMidY(self.frame));
SKNode *top = [SKNode node];
top.position = CGPointMake(0, 22.5);
top.name = @”top”;
for (int i=0; i<3; i++ ) {
int type[] = {0, 1, 0};
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:color size:parts[type[i]]];
n.position = CGPointMake((i – 1) * 15 , i != 1 ? 0 : 10);
[top addChild:n];
}
SKNode *plus = [SKNode node];
plus.name = @”plus”;
for (int i=0; i<2; i++) {
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:color size:parts[2]];
n.zRotation = M_PI/2.0 * i;
[plus addChild:n];
}
SKNode *bottom = [SKNode node];
bottom.position = CGPointMake(0, –22.5);
bottom.name = @”bottom”;
for (int i=0; i<3; i++ ) {
int type[] = {0, 1, 0};
SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:color size:parts[type[i]]];
n.position = CGPointMake((i – 1) * 15 , i != 1 ? 0 : –10);
[bottom addChild:n];
}
[box addChild:top];
[box addChild:plus];
[box addChild:bottom];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
[self enumerateChildNodesWithName:@”box” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
[[node childNodeWithName:@”top”] runAction:[SKAction moveToY:100 duration:2.0]];
[[node childNodeWithName:@”bottom”] runAction:[SKAction moveToY:-100 duration:2.0]];
}
}];
}
@end
@implementation ViewController
– (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[BoxScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end