iPhone hex ボックス

アルファベットをhexに変換する箱を表示するiPhoneアプリのサンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface FunctionBoxScene : SKScene

@property (nonatomic, strong) SKNode *select;

@end

@implementation FunctionBoxScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor purpleColor];

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

    

    [self createFunctionBoxHex];

    [self createBalls];

}

– (void)createFunctionBoxHex

{

    SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(100, 100)];

    box.name = @”box”;

    box.position = CGPointMake(CGRectGetMidX(self.frame), 180);

    box.zPosition = 10;

    [self addChild:box];

    

    box.physicsBody = [SKPhysicsBody bodyWithBodies:@[[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 100) center:CGPointMake(-48, 0)], [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(5, 100) center:CGPointMake(48, 0)]]];

    box.physicsBody.dynamic = NO;

    

    SKLabelNode *l = [SKLabelNode node];

    l.text = @”hex”;

    l.fontSize = 50;

    l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

    [box addChild:l];

}

– (void)createBalls

{

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

    

    NSArray *word = [@”a b c d e f g h i j” componentsSeparatedByString:@” “];

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

        SKShapeNode *ball = [SKShapeNode node];

        ball.name = @”ball”;

        ball.path = path.CGPath;

        ball.position = CGPointMake(i * 20 + 20, 50 * (i % 2));

        [self addChild:ball];

        ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];

        

        SKLabelNode *l = [SKLabelNode node];

        l.text = word[i];

        l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;

        [ball addChild:l];

    }

}

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

{

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

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

        if ([node containsPoint:p]) {

            self.select = node;

            self.select.physicsBody.affectedByGravity = NO;

        }

    }];

}

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

{

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

    self.select.position = p;

}

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

{

    self.select.physicsBody.affectedByGravity = YES;

    self.select = nil;

}

– (void)update:(NSTimeInterval)currentTime

{

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

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

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

            node.name = @”hex”;

            

            SKLabelNode *l = node.children[0];

            l.fontSize = 14;

            l.text = [NSString stringWithFormat:@”0x%x”, [l.text UTF8String][0]];

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end