iPhoneドロップカウンタ

落ちてくる水玉がコップに入った数を数えるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface DropScene : SKScene

@end

@implementation DropScene

– (void)didMoveToView:(SKView *)view

{

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    self.physicsBody.dynamic = NO;

    [self createCup];

    [self createRoof];

    [self createCounterLabel];

    [self createStartButton];

}

– (void)createCup

{

    for (int i=0; i<2; i++) {

        UIBezierPath *path = [UIBezierPath bezierPath];

        [path moveToPoint:CGPointMake(-50, 50)];

        [path addLineToPoint:CGPointMake(-50, –50)];

        [path addLineToPoint:CGPointMake(50, –50)];

        [path addLineToPoint:CGPointMake(50, 50)];

        SKShapeNode *cup = [SKShapeNode node];

        cup.name = [NSString stringWithFormat:@”cup%d”, i];

        cup.path = path.CGPath;

        cup.position = CGPointMake(80 + i*160, 100);

        [self addChild:cup];

        

        SKPhysicsBody *l = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, 100) center:CGPointMake(-50, 0)];

        SKPhysicsBody *r = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(1, 100) center:CGPointMake(50, 0)];

        SKPhysicsBody *b = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(100, 1) center:CGPointMake(0, –50)];

        cup.physicsBody = [SKPhysicsBody bodyWithBodies:@[l, r, b]];

        cup.physicsBody.dynamic = NO;

    }

}

– (void)createRoof

{

    for (int i=0; i<20; i++) {

        SKSpriteNode *bar = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:(i % 10) * 0.1 saturation:0.9 brightness:0.9 alpha:1.0] size:CGSizeMake(32, 10)];

        bar.name = @”bar”;

        float x = (i % 10) * 32 + 16;

        float y = CGRectGetMaxY(self.frame) – (i / 10 + 1) * 100;

        bar.position = CGPointMake(x, y);

        [self addChild:bar];

        bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];

        bar.physicsBody.dynamic = NO;

    }

}

– (void)createDrop

{

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-5, –5, 10, 10)];

    SKShapeNode *drop = [SKShapeNode node];

    drop.position = CGPointMake((arc4random() % 300) + 10, CGRectGetMaxY(self.frame));

    drop.fillColor = [SKColor colorWithRed:0.7 green:0.7 blue:1.0 alpha:1.0];

    drop.name = @”drop”;

    drop.path = path.CGPath;

    drop.lineWidth = 0;

    [self addChild:drop];

    

    drop.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];

}

– (void)createCounterLabel

{

    for (int i=0; i<2; i++) {

        SKLabelNode *l = [SKLabelNode node];

        l.name = [NSString stringWithFormat:@”counter%d”, i];

        l.position = CGPointMake(80 + 160 * i, 200);

        l.fontName = @”chalkduster”;

        l.fontSize = 50;

        l.text = @”0″;

        [self addChild:l];

    }

}

– (void)createStartButton

{

    SKLabelNode *startBtn = [SKLabelNode node];

    startBtn.name = @”start”;

    startBtn.position = CGPointMake(160, 300);

    startBtn.text = @”start”;

    [self addChild:startBtn];

}

– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    SKNode *startbtn = [self childNodeWithName:@”start”];

    if ([startbtn containsPoint:[[touches anyObject] locationInNode:self]]) {

        [startbtn removeFromParent];

        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createDrop) userInfo:nil repeats:YES];

        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(openRoof) userInfo:nil repeats:YES];

    }

}

– (void)openRoof

{

    [self enumerateChildNodesWithName:@”bar” usingBlock:^(SKNode *node, BOOL *stop) {

        SKSpriteNode *bar = (SKSpriteNode *)node;

        

        if (arc4random() % 3) {

            [bar runAction:[SKAction fadeInWithDuration:0.5]];

            bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bar.size];

            bar.physicsBody.dynamic = NO;

        } else {

            [bar runAction:[SKAction fadeOutWithDuration:0.5]];

            bar.physicsBody = nil;

        }

    }];

}

– (void)didSimulatePhysics

{

    __block NSUInteger countA = 0;

    __block NSUInteger countB = 0;

    

    SKNode *cupA = [self childNodeWithName:@”cup0″];

    SKNode *cupB = [self childNodeWithName:@”cup1″];

    [self enumerateChildNodesWithName:@”drop” usingBlock:^(SKNode *node, BOOL *stop) {

        if ([cupA containsPoint:node.position]) {

            countA++;

        } else if ([cupB containsPoint:node.position]) {

            countB++;

        }

    }];

    

    SKLabelNode *counterA = (SKLabelNode *)[self childNodeWithName:@”counter0″];

    SKLabelNode *counterB = (SKLabelNode *)[self childNodeWithName:@”counter1″];

    counterA.text = [@(countA) stringValue];

    counterB.text = [@(countB) stringValue];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[DropScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

@end