iPhone 円合わせ

タイミングよく円が重なったところでタップして遊ぶようなiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

@import SpriteKit;

@interface CircleScene : SKScene

@end

@implementation CircleScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1];

    [self createBox];

    

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

}

– (void)createBox

{

    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:60 startAngle:0 endAngle:2.0*M_PI clockwise:NO];

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

        SKColor *color = i ? [SKColor whiteColor] : [SKColor blackColor];

        SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(200, 200)];

        box.position = CGPointMake(160, i * 200 + 140);

        [self addChild:box];

        

        SKShapeNode *mark = [SKShapeNode node];

        mark.path = path.CGPath;

        mark.fillColor = [SKColor clearColor];

        mark.strokeColor = !i ? [SKColor whiteColor] : [SKColor blackColor];

        mark.lineWidth = 20;

        [box addChild:mark];

    }

}

– (void)createHoop

{

    SKNode *hoop = [SKNode node];

    hoop.name = @”hoop”;

    float cnt = 10;

    float dAngle = 2.0 * M_PI / cnt;

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

        UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:200 startAngle:dAngle * i endAngle:dAngle * (i + 1) clockwise:YES];

        SKShapeNode *arc = [SKShapeNode node];

        arc.path = path.CGPath;

        arc.fillColor = [SKColor clearColor];

        arc.strokeColor = [[SKColor colorWithHue:0.1*i saturation:0.8 brightness:1 alpha:1] colorWithAlphaComponent:0.8];

        arc.lineWidth = 40;

        [hoop addChild:arc];

    }

    hoop.name = @”hoop”;

    hoop.position = (arc4random() % 2) == 0 ? CGPointMake(160, 140) : CGPointMake(160, 340);

    [self addChild:hoop];

    

    SKAction *small = [SKAction scaleTo:0.01 duration:4.0];

    [hoop runAction:small completion:^{

        [hoop removeFromParent];

    }];

}

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

{

    CGPoint p = [[touches anyObject] locationInNode:self];

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

        if ([node containsPoint:p]) {

            *stop = YES;

            

            [node removeAllActions];

            

            if (node.xScale > 0.25 && node.xScale < 0.35) {

                SKAction *turn = [SKAction rotateToAngle:4.0*M_PI duration:1.0];

                [node runAction:turn completion:^{

                    [node removeFromParent];

                }];

            } else {

                [node runAction:[SKAction waitForDuration:1.0] completion:^{

                    [node removeFromParent];

                }];

            }

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

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

    [spriteView presentScene:scene];

}

@end