iPhone数縦横

パネルに描いてある数だけリングを進めてゴールを目指すiPhoneアプリのサンプルコードを描いてみます。

@import SpriteKit;

#import “ViewController.h”

@interface MoveScene : SKScene

@end

@implementation MoveScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor colorWithRed:0.9 green:0.9 blue:0.8 alpha:1];

    [self createNumbers];

    [self createGoal];

    [self createPlayerRing];

    [self createReset];

}

– (void)createNumbers

{

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

        float x = (i % 6) * 50 + 35;

        float y = (i / 6) * 50 + 120;

        

        int num = (arc4random() % 4) + 1;

        SKSpriteNode *n = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:num * 0.18 saturation:0.8 brightness:0.7 alpha:1] size:CGSizeMake(50, 50)];

        n.name = [@(i) stringValue];

        n.position = CGPointMake(x, y);

        [self addChild:n];

        

        SKLabelNode *l = [SKLabelNode node];

        l.text = [@(num) stringValue];

        l.position = CGPointMake(0, 0);

        l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

        l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;

        l.fontColor = [SKColor whiteColor];

        [n addChild:l];

        

    }

}

– (void)createGoal

{

    NSInteger num = (arc4random() % 36);

    SKNode *target = [self childNodeWithName:[@(num) stringValue]];

    

    SKSpriteNode *goal = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(50, 50)];

    goal.name = @”goal”;

    goal.position = target.position;

    [self addChild:goal];

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

        SKSpriteNode *line = [SKSpriteNode spriteNodeWithColor:(i%2) ? [SKColor whiteColor] : [SKColor blackColor] size:CGSizeMake(50, 10)];

        line.position = CGPointMake(0, 10 * i –20);

        [goal addChild:line];

    }

}

– (void)createPlayerRing

{

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

    SKShapeNode *ring = [SKShapeNode node];

    ring.name = @”ring”;

    ring.path = path.CGPath;

    ring.lineWidth = 4;

    [self addChild:ring];

    

    

    SKNode *target = [self childNodeWithName:@”22″];

    ring.position = target.position;

}

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

{

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

    NSArray *hits = [self nodesAtPoint:p];

    for (SKNode *hit in hits) {

        if (hit.name.length > 0 && hit.name.length < 3) {

            [self moveRing:hit];

            return;

        }

        

        if ([hit.name isEqual:@”reset”]) {

            [self reset];

            return;

        }

    }

}

– (void)moveRing:(SKNode *)number

{

    SKNode *ring = [self childNodeWithName:@”ring”];

    NSInteger n = [((SKLabelNode *)number.children[0]).text integerValue];

    if (fabs(ring.position.y – number.position.y) < 5) {

        float dx = (ring.position.x – number.position.x > 0) ? -n : n;

        SKAction *move = [SKAction moveByX:dx * 50 y:0 duration:0.5 * n];

        [ring runAction:move];

    }

    else if (fabs(ring.position.x – number.position.x) < 5) {

        float dy = (ring.position.y – number.position.y > 0) ? -n : n;

        SKAction *move = [SKAction moveByX:0 y:dy * 50 duration:0.5 * n];

        [ring runAction:move];

    }

}

– (void)createReset

{

    SKLabelNode *reset = [SKLabelNode node];

    reset.name = @”reset”;

    reset.text = @”reset”;

    reset.position = CGPointMake(CGRectGetMidX(self.frame), 50);

    [self addChild:reset];

}

– (void)reset

{

    [self.children makeObjectsPerformSelector:@selector(removeFromParent)];

    

    [self createNumbers];

    [self createGoal];

    [self createPlayerRing];

    [self createReset];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

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

    [spriteView presentScene:scene];

}

@end