iPhoneカタカタおもちゃ

カタカタとピンの間を落ちてくるおもちゃのiPhoneアプリサンプルコードを描いてみます。


動作イメージ

XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface KataKataScene : SKScene

@property BOOL contentCreated;

@end

@implementation KataKataScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [self color:4];

    [self createPlate];

    [self createPins];

}

– (void)createPlate

{

    SKNode *node = [SKNode node];

    node.position = CGPointMake(120, 500);

    [self addChild:node];

    SKSpriteNode *barA = [SKSpriteNode spriteNodeWithColor:[self color:0] size:CGSizeMake(30, 80)];

    barA.name = @”bar”;

    barA.position = CGPointMake(40, 10);

    barA.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:barA.size];

    [node addChild:barA];

    

    SKSpriteNode *barB = [SKSpriteNode spriteNodeWithColor:[self color:0] size:CGSizeMake(80, 10)];

    barB.position = CGPointMake(40, 40);

    barB.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:barB.size];

    barB.physicsBody.friction = 0;

    [node addChild:barB];

    

    SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:barA.physicsBody bodyB:barB.physicsBody anchor:barA.position];

    [self.physicsWorld addJoint:joint];

    

}

– (void)createPins

{

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

        SKShapeNode *nodeA = [SKShapeNode node];

        nodeA.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-5, –5, 10, 10)].CGPath;

        nodeA.position = CGPointMake(120, i * 70 + 50);

        nodeA.fillColor = [self color:(arc4random()%3) + 1];

        nodeA.strokeColor = nodeA.fillColor;

        [self addChild:nodeA];

        nodeA.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

        nodeA.physicsBody.dynamic = NO;

        

        SKShapeNode *nodeB = [SKShapeNode node];

        nodeB.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-5, –5, 10, 10)].CGPath;

        nodeB.position = CGPointMake(190, i * 70 + 15);

        nodeB.fillColor = [self color:(arc4random()%3) + 1];

        nodeB.strokeColor = nodeB.fillColor;

        [self addChild:nodeB];

        nodeB.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];

        nodeB.physicsBody.dynamic = NO;

    }

}

– (void)didSimulatePhysics

{

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

    if ([self convertPoint:bar.position fromNode:bar.parent].y < – 50) {

        [[bar parent] removeFromParent];

        [self createPlate];

    }

}

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0xD99A9A);

        case 1:

            return UIColorHex(0xD95B89);

        case 2:

            return UIColorHex(0x012840);

        case 3:

            return UIColorHex(0x025959);

        case 4:

            return UIColorHex(0xF2E3B3);

        default:

            break;

    }

    return nil;

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end