iPhoneハッシュ箱

箱に描いてある数から簡易的にハッシュ値を求めて並べるiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface BoxScene : SKScene

@end

@implementation BoxScene

– (void)didMoveToView:(SKView *)view

{

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

    [self createBlock];

    [self createHole];

}

– (void)createBlock

{

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

        int rand = arc4random() % 9;

        float hue = rand * 0.1 + i * 0.01;

        SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithHue:hue saturation:0.8 brightness:1.0 alpha:1.0] size:CGSizeMake(30, 30)];

        box.name = @”box”;

        box.position = CGPointMake((i % 10) * 32 + 15, CGRectGetMaxY(self.frame) – 50 * (i / 10) – 100);

        [self addChild:box];

        

        SKLabelNode *l = [SKLabelNode node];

        l.text = [NSString stringWithFormat:@”%d”, (int)(100 * hue)];

        l.fontSize = 15;

        l.fontColor = [SKColor blackColor];

        l.userInteractionEnabled = NO;

        [box addChild:l];

    }

}

– (void)createHole

{

    SKNode *bar = [SKNode node];

    bar.name = @”bar”;

    bar.position = CGPointMake(160, CGRectGetMidY(self.frame));

    

    SKSpriteNode *r = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 50)];

    r.position = CGPointMake(-180, 0);

    [bar addChild:r];

    

    SKSpriteNode *l = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 50)];

    l.position = CGPointMake(180, 0);

    [bar addChild:l];

    

    SKSpriteNode *cover = [[SKSpriteNode alloc] initWithColor:[SKColor lightGrayColor] size:CGSizeMake(30, 5)];

    cover.position = CGPointMake(0, –20);

    [bar addChild:cover];

    

    [self addChild:bar];

    

    

    bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:cover.size center:cover.position];

    bar.physicsBody.dynamic = NO;

}

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

{

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

    NSArray *nodes = [self nodesAtPoint:p];

    SKNode *box = [SKNode node];

    for (SKNode *n in nodes) {

        if ([n.name isEqual:@”box”]) {

            box = n;

        }

    }

    

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

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

        [bar runAction:[SKAction moveToX:box.position.x duration:0.5] completion:^{

            box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:((SKSpriteNode*)box).size];

            SKAction *joint = [SKAction runBlock:^{

                SKPhysicsJoint *j = [SKPhysicsJointFixed jointWithBodyA:box.physicsBody bodyB:bar.physicsBody anchor:box.position];

                [self.physicsWorld addJoint:j];

            }];

            

            SKPhysicsBody *tmpBody = bar.physicsBody;

            SKAction *separate = [SKAction runBlock:^{

                [self.physicsWorld removeAllJoints];

                bar.physicsBody = nil;

                box.physicsBody.allowsRotation = NO;

            }];

            

            int hash = [self myHash:[((SKLabelNode *)[box children][0]).text intValue]];

            

            float x = hash * 30 + 15;

            [bar runAction:[SKAction sequence:@[[SKAction waitForDuration:0.6], joint, [SKAction moveToX:x duration:0.5], [SKAction waitForDuration:0.6], separate, [SKAction waitForDuration:0.3]]] completion:^{

                bar.physicsBody = tmpBody;

                box.name = @””;

            }];

    

        }];

    }

}

– (int)myHash:(int)number

{

    return (number / 10) % 10;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end