iPhoneつむピラミッド

積んであそぶおもちゃ。みたいなiPhoneアプリのサンプルコードを描いてみます。

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface PyramidScene : SKScene

@property (nonatomic, weak) SKNode *anchor;

@end

@implementation PyramidScene

– (void)didMoveToView:(SKView *)view

{

    self.backgroundColor = [SKColor yellowColor];

    

    [self createWall];

    

    SKNode *fig = [self createFig];

    fig.position = CGPointMake(160, 50);

}

– (void)createWall

{

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

    self.physicsBody.dynamic = NO;

}

– (SKNode *)createFig

{

    SKNode *fig = [SKNode node];

    fig.name = @”figure”;

    [self addChild:fig];

    

    SKColor *color = [SKColor colorWithWhite:(arc4random() % 2) * 0.5 alpha:1];

    

    UIBezierPath *headPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 20) radius:18 startAngle:0 endAngle:2.0*M_PI clockwise:NO];

    SKShapeNode *head = [SKShapeNode node];

    head.path = headPath.CGPath;

    head.fillColor = color;

    head.lineWidth = 0;

    [fig addChild:head];

    SKSpriteNode *hand = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(70, 15)];

    [fig addChild:hand];

    

    SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(30, 30)];

    body.position = CGPointMake(0, –10);

    [fig addChild:body];

    

    SKSpriteNode *footL = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(22, 15)];

    footL.position = CGPointMake(25, –30);

    [fig addChild:footL];

    

    SKSpriteNode *footR = [SKSpriteNode spriteNodeWithColor:color size:CGSizeMake(22, 15)];

    footR.position = CGPointMake(-25, –30);

    [fig addChild:footR];

    

    fig.physicsBody = [SKPhysicsBody bodyWithBodies:@[[SKPhysicsBody bodyWithPolygonFromPath:headPath.CGPath],[SKPhysicsBody bodyWithRectangleOfSize:hand.size center:hand.position], [SKPhysicsBody bodyWithRectangleOfSize:body.size center:body.position], [SKPhysicsBody bodyWithRectangleOfSize:footL.size center:footL.position], [SKPhysicsBody bodyWithRectangleOfSize:footR.size center:footR.position]]];

    

    return fig;

}

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

{

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

    SKSpriteNode *anchor = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(10, 10)];

    [self addChild:anchor];

    anchor.position = p;

    anchor.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];

    anchor.physicsBody.dynamic = NO;

    self.anchor = anchor;

    __block SKNode *selected;

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

        if ([node containsPoint:p]) {

            selected = node;

        }

    }];

    if (!selected) {

        SKNode *figure = [self createFig];

        figure.position = p;

        selected = figure;

    }

    

    SKPhysicsJointPin *pin = [SKPhysicsJointPin jointWithBodyA:anchor.physicsBody bodyB:selected.physicsBody anchor:anchor.position];

    [self.physicsWorld addJoint:pin];

}

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

{

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

    self.anchor.position = p;

}

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

{

    [self.physicsWorld removeAllJoints];

    [self.anchor removeFromParent];

    self.anchor = 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 = [[PyramidScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end