iPhone起き上がり小法師

起き上がり小法師をふりふりして遊ぶiPhoneアプリのサンプルコードを描いてみます。

こんな感じで動きます

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface OkiagariScene : SKScene

@property BOOL contentCreated;

@end

@implementation OkiagariScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor darkGrayColor];

    [self createWall];

    [self createOkiagariKobosi];

}

– (void)createWall

{

    UIBezierPath *wallPath = [UIBezierPath bezierPathWithRect:self.frame];

    SKShapeNode *wall = [SKShapeNode node];

    wall.lineWidth = 5;

    wall.strokeColor = [SKColor brownColor];

    wall.path = wallPath.CGPath;

    [self addChild:wall];

    

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

}

– (void)createOkiagariKobosi

{

    float w = 60;

    float h = 150;

    UIBezierPath *headPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-w/2.0, -w/2.0, w, w)];

    SKShapeNode *head = [SKShapeNode node];

    head.name = @”head”;

    head.fillColor = [SKColor redColor];

    head.path = headPath.CGPath;

    head.position = CGPointMake(CGRectGetMidX(self.frame), h + w/2.0);

    [self addChild:head];

    head.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:w/2.0];

    

    SKLabelNode *label = [SKLabelNode labelNodeWithFontNamed:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline].fontName];

    label.text = @”^_^”;

    label.fontSize = 25;

    [head addChild:label];

    

    w *= 1.4142;

    UIBezierPath *bodyPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-w/2.0, -w/2.0, w, w)];

    SKShapeNode *body = [SKShapeNode node];

    body.fillColor = head.fillColor;

    body.path = bodyPath.CGPath;

    body.position = CGPointMake(CGRectGetMidX(self.frame), h – w/2.0);

    [self addChild:body];

    body.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:w/2.0];

    

    SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:head.physicsBody bodyB:body.physicsBody anchor:CGPointMake(CGRectGetMidX(self.frame), h)];

    [self.physicsWorld addJoint:joint];

    SKSpriteNode *weight = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(25, 25)];

    weight.position = CGPointMake(0, – 25);

    [body addChild:weight];

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

    weight.physicsBody.density = 30.0;

    

    SKPhysicsJointFixed *joint2 = [SKPhysicsJointFixed jointWithBodyA:body.physicsBody bodyB:weight.physicsBody anchor:weight.position];

    [self.physicsWorld addJoint:joint2];

    

    [weight.physicsBody applyAngularImpulse:0.1];

}

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

{

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

    

    SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:[SKColor orangeColor] size:CGSizeMake(10, 10)];

    node.name = @”anchor”;

    node.position = p;

    [self addChild:node];

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

    node.physicsBody.dynamic = NO;

    

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

    

    SKPhysicsJointLimit *limit = [SKPhysicsJointLimit jointWithBodyA:node.physicsBody bodyB:head.physicsBody anchorA:node.position anchorB:head.position];

    [self.physicsWorld addJoint:limit];

}

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

{

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

    

    SKNode *node = [self childNodeWithName:@”anchor”];

    SKAction *move = [SKAction moveTo:p duration:0];

    [node runAction:move];

}

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

{

    SKNode *node = [self childNodeWithName:@”anchor”];

    [self.physicsWorld removeJoint:node.physicsBody.joints[0]];

    [node removeFromParent];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end