10進数を2で割ったあまりをスロープに転がして2進数を表示するiPhoneアプリのサンプルコードを描いてみます。
#import “ViewController.h”
@import SpriteKit;
@interface BinaryScene : SKScene
@end
@implementation BinaryScene
– (void)didMoveToView:(SKView *)view
{
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
[self createSlope];
[self createDecimalFeild];
}
– (void)createSlope
{
for (int i=0; i<8; i++) {
SKSpriteNode *slopeA = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(120, 2)];
slopeA.position = CGPointMake(180, 130 + i * 35);
slopeA.zRotation = –M_PI * 0.1;
[self addChild:slopeA];
slopeA.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:slopeA.size];
slopeA.physicsBody.dynamic = NO;
}
SKSpriteNode *slopeB = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(250, 2)];
slopeB.position = CGPointMake(180, 60);
slopeB.zRotation = M_PI * 0.02;
[self addChild:slopeB];
slopeB.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:slopeB.size];
slopeB.physicsBody.dynamic = NO;
SKSpriteNode *slopeC = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(50, 2)];
slopeC.position = CGPointMake(25, 30);
slopeC.zRotation = –M_PI * 0.2;
[self addChild:slopeC];
slopeC.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:slopeC.size];
slopeC.physicsBody.dynamic = NO;
}
– (void)createDecimalFeild
{
SKLabelNode *l = [SKLabelNode node];
l.name = @”decimal”;
l.text = @”234″;
l.position = CGPointMake(70, 400);
[self addChild:l];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKLabelNode *decimal = (SKLabelNode *)[self childNodeWithName:@”decimal”];
int q = [decimal.text intValue] / 2;
int r = [decimal.text intValue] % 2;
decimal.fontColor = [SKColor darkGrayColor];
decimal.text = [decimal.text stringByAppendingString:@”/2″];
SKLabelNode *next = [SKLabelNode node];
next.position = CGPointMake(decimal.position.x, decimal.position.y – 35);
next.text = [@(q) stringValue];
next.name = decimal.name;
decimal.name = @””;
[self addChild:next];
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointZero radius:15 startAngle:0 endAngle:2.0*M_PI clockwise:NO];
SKShapeNode *binary = [SKShapeNode node];
binary.path = path.CGPath;
binary.position = CGPointMake(decimal.position.x + 30, decimal.position.y);
[self addChild:binary];
SKLabelNode *l = [SKLabelNode node];
l.text = [@(r) stringValue];
l.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
l.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
l.fontSize = 20;
[binary addChild:l];
binary.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:14];
[binary.physicsBody applyImpulse:CGVectorMake(10, 3)];
}
@end
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
SKView *spriteView = [[SKView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:spriteView];
SKScene *scene = [[BinaryScene alloc] initWithSize:spriteView.frame.size];
[spriteView presentScene:scene];
}
@end