
0と1のボタンをポチポチしてBase64エンコードで遊ぶiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface ViewController () <SKPhysicsContactDelegate>
@property (nonatomic, weak) SKScene *scene;
@end
#define Base64 @“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
[self setupScene];
[self createBitButton];
}
– (void)setupScene {
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [SKScene sceneWithSize:spriteView.frame.size];
scene.backgroundColor = [self color:0];
[spriteView presentScene:scene];
scene.physicsWorld.contactDelegate = self;
self.scene = scene;
SKSpriteNode *stopper = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(1, CGRectGetMaxY(self.view.bounds))];
stopper.position= CGPointMake(-1, CGRectGetMidY(self.view.bounds));
[self.scene addChild:stopper];
stopper.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:stopper.size];
stopper.physicsBody.dynamic = NO;
stopper.physicsBody.restitution = 0;
}
– (void)createBitButton {
for (int i=0; i<2; i++) {
SKSpriteNode *btn = [SKSpriteNode spriteNodeWithColor:[self color:1] size:CGSizeMake(80, 80)];
btn.position = CGPointMake(CGRectGetMaxX(self.view.bounds) * (0.25 + i * 0.5) , 80);
btn.name = [NSString stringWithFormat:@”btn%d”,i];
[self.scene addChild:btn];
SKLabelNode *l = [SKLabelNode labelNodeWithText:[NSString stringWithFormat:@”%d”, i]];
l.userInteractionEnabled = NO;
l.fontSize = 50;
l.fontColor = [self color:3];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
[btn addChild:l];
}
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
float s = CGRectGetMaxX(self.view.bounds) / 6.0;
CGPoint p = [[touches anyObject] locationInNode:self.scene];
[self.scene enumerateChildNodesWithName:@”btn[0-1]” usingBlock:^(SKNode *node, BOOL *stop) {
if ([node containsPoint:p]) {
SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[self color:3] size:CGSizeMake(s, s)];
box.name = [NSString stringWithFormat:@”box%@”, [node.name substringFromIndex:3]];
box.position = CGPointMake(s * 7.0, 200);
[self.scene addChild:box];
SKLabelNode *l = [SKLabelNode labelNodeWithText:[node.name substringFromIndex:3]];
l.userInteractionEnabled = NO;
l.fontSize = 30;
l.fontColor = [self color:2];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
[box addChild:l];
box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];
box.physicsBody.categoryBitMask = 0x1 << 1;
box.physicsBody.contactTestBitMask = 0x1 << 1;
box.physicsBody.affectedByGravity = NO;
box.physicsBody.restitution = 0;
box.physicsBody.allowsRotation = NO;
[box.physicsBody applyImpulse:CGVectorMake(-60, 0)];
}
}];
}
– (void)didBeginContact:(SKPhysicsContact *)contact
{
contact.bodyA.dynamic = NO;
contact.bodyB.dynamic = NO;
contact.bodyB.contactTestBitMask = 0x1 << 2;
NSMutableArray *binary = [NSMutableArray array];
[self.scene enumerateChildNodesWithName:@”box[0-1]” usingBlock:^(SKNode *node, BOOL *stop) {
[binary addObject:[node.name substringFromIndex:3]];
if (binary.count == 6) {
[self showBase64:binary];
}
}];
}
– (void)showBase64:(NSArray *)binary {
int index = 0;
for (int i=0; i < binary.count; i++) {
index += [binary[i] intValue] * pow(2, binary.count – 1 – i);
}
float w = CGRectGetMaxX(self.view.bounds) * 0.8;
NSString *str = [NSString stringWithFormat:@”%c”, [Base64 characterAtIndex:index]];
SKSpriteNode *box = [SKSpriteNode spriteNodeWithColor:[self color:4] size:CGSizeMake(w, w)];
box.position = CGPointMake(w * 0.5 / 0.8, CGRectGetMaxY(self.view.bounds) * 1.4);
[self.scene addChild:box];
SKLabelNode *l = [SKLabelNode labelNodeWithText:str];
l.userInteractionEnabled = NO;
l.fontSize = w * 0.6;
l.fontColor = [self color:2];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
[box addChild:l];
box.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:box.size];
box.physicsBody.categoryBitMask = 0x1 << 3;
[self.scene enumerateChildNodesWithName:@”box[0-1]” usingBlock:^(SKNode *node, BOOL *stop) {
node.name = @”boxused”;
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[box removeFromParent];
[self.scene enumerateChildNodesWithName:@”boxused” usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
});
}
#define ColorHex(rgb) [UIColor colorWithRed:((rgb & 0xFF0000) >> 16) / 255.0 green:((rgb & 0xFF00) >> 8)/255.0 blue:(rgb & 0xFF) / 255.0 alpha:1.0]
– (UIColor *)color:(int)i {
switch (i) {
case 0: return ColorHex(0xEA6045);
case 1: return ColorHex(0xF8CA4D);
case 2: return ColorHex(0xF5E5C0);
case 3: return ColorHex(0x3F5666);
case 4: return ColorHex(0x2F3440);
default:
break;
}
return nil;
}
@end