ポケットをたたくと、ビスケットがふえるアレのiPhoneアプリサンプルコードを描いてみます。
今回利用した画像
動かすとこんな感じです
サンプルコード
#import “ViewController.h”
#import <SpriteKit/SpriteKit.h>
@interface PocketScene : SKScene
@property BOOL contentCreated;
@end
@implementation PocketScene
– (void)didMoveToView:(SKView *)view
{
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
}
– (void)createSceneContents
{
self.physicsWorld.speed = 0.5;
[self createShirtWithPocket];
[self makeCookie];
[self createPlusButton];
}
– (void)createShirtWithPocket
{
SKSpriteNode *shirt = [SKSpriteNode spriteNodeWithImageNamed:@”tshirt”];
shirt.position = CGPointMake(CGRectGetMidX(self.frame), 180);
[self addChild:shirt];
SKSpriteNode *pocket = [SKSpriteNode spriteNodeWithImageNamed:@”pocket”];
pocket.name = @”pocket”;
pocket.position = shirt.position;
pocket.zPosition = 2;
[self addChild:pocket];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0,50) radius:75 startAngle:0 endAngle:M_PI clockwise:NO];
// SKShapeNode *shape = [SKShapeNode node];
// shape.path = path.CGPath;
// shape.strokeColor = [SKColor yellowColor];
// [self addChild:shape];
pocket.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path.CGPath];
}
– (void)makeCookie
{
SKSpriteNode *cookie = [[SKSpriteNode alloc] initWithImageNamed:@”cookie”];
cookie.name = @”cookie”;
cookie.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) – 50);
cookie.zRotation = M_PI * 0.1;
[self addChild:cookie];
cookie.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 50)];
}
– (void)createPlusButton
{
SKLabelNode *plus = [SKLabelNode node];
plus.name = @”plus”;
plus.text = @”<+>”;
plus.position = CGPointMake(CGRectGetMidX(self.frame), 30);
[self addChild:plus];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInNode:self];
SKNode *plus = [self childNodeWithName:@”plus”];
if ([plus containsPoint:p]) {
[self makeCookie];
}
SKNode *pocket = [self childNodeWithName:@”pocket”];
if ([pocket containsPoint:p]) {
[self magicalCookie];
}
}
– (void)magicalCookie
{
NSMutableArray *cookies = [[NSMutableArray alloc] init];
[self enumerateChildNodesWithName:@”cookie” usingBlock:^(SKNode *node, BOOL *stop) {
SKNode *copyCookie = [node copy];
copyCookie.name = @”animationCookie”;
[self addChild:copyCookie];
node.name = @”animationCookie”;
[cookies addObjectsFromArray:@[copyCookie, node]];
}];
for (int i=0; i<[cookies count]; i++) {
float x = arc4random() % 320;
SKAction *delay = [SKAction waitForDuration:i * 0.2];
SKAction *flyaway = [SKAction moveTo:CGPointMake(x, 500) duration:1.0];
SKAction *rename = [SKAction runBlock:^{
[cookies[i] setName:@”cookie”];
}];
[cookies[i] runAction:[SKAction sequence:@[delay, flyaway, rename]]];
}
}
– (void)didSimulatePhysics
{
[self enumerateChildNodesWithName:@”cookie” usingBlock:^(SKNode *node, BOOL *stop) {
if (node.position.y < –40) {
[node removeFromParent];
}
}];
}
@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 = [[PocketScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end