iPhoneしゃちほこ

シャチホコをお城の天守閣にクレーンでのっけるというかんじのiPhoneアプリのサンプルコードを描いてみます。

今回使った画像


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface CastleScene : SKScene

@property (nonatomic, strong) SKShapeNode *wire;

@property int direction;

@end

@implementation CastleScene

– (void)didMoveToView:(SKView *)view

{

    [self createSceneContents];

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor colorWithRed:0.8 green:0.8 blue:1.0 alpha:1];

    [self createGround];

    [self createCastle];

    [self createShachihoko];

    [self createClaw];

}

– (void)createGround

{

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(CGRectGetMaxX(self.frame), 30)];

    ground.position = CGPointMake(CGRectGetMidX(self.frame), 15);

    [self addChild:ground];

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

    ground.physicsBody.dynamic = NO;

}

– (void)createCastle

{

    SKSpriteNode *castle = [SKSpriteNode spriteNodeWithImageNamed:@”castle”];

    castle.position = CGPointMake(CGRectGetMaxX(self.frame) – 100, 100);

    [self addChild:castle];

    

    UIBezierPath *trapezoid = [UIBezierPath bezierPath];

    float w = castle.size.width * 0.8;

    float h = castle.size.height;

    [trapezoid moveToPoint:CGPointMake(-w/2.0, -h/2.0)];

    [trapezoid addLineToPoint:CGPointMake(w/2.0, -h/2.0)];

    [trapezoid addLineToPoint:CGPointMake(w/6.0, h/2.010)];

    [trapezoid addLineToPoint:CGPointMake(-w/6.0, h/2.010)];

    [trapezoid closePath];

    SKShapeNode *c = [SKShapeNode node];

    c.position = castle.position;

    c.strokeColor = [SKColor redColor];

    c.path = trapezoid.CGPath;

    [self addChild:c];

    

    castle.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:trapezoid.CGPath];

    castle.physicsBody.dynamic = NO;

}

– (void)createShachihoko

{

    SKSpriteNode *shachihoko = [SKSpriteNode spriteNodeWithImageNamed:@”shachihoko”];

    shachihoko.position = CGPointMake(150, 50);

    [self addChild:shachihoko];

    

    shachihoko.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:shachihoko.size.width/2.0];

    shachihoko.physicsBody.allowsRotation = NO;

    shachihoko.physicsBody.density = 0.01;

}

– (void)createClaw

{

    SKSpriteNode *clawA = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(5, 40)];

    clawA.name = @”clawA”;

    clawA.anchorPoint = CGPointMake(0.5, 1.0);

    clawA.position = CGPointMake(110, 200);

    clawA.zRotation = M_PI * 0.1;

    [self addChild:clawA];

    

    SKSpriteNode *clawB = [SKSpriteNode spriteNodeWithColor:[SKColor darkGrayColor] size:CGSizeMake(5, 40)];

    clawB.name = @”clawB”;

    clawB.anchorPoint = CGPointMake(0.5, 1.0);

    clawB.position = CGPointMake(180, 200);

    clawB.zRotation = –M_PI * 0.1;

    [self addChild:clawB];

    

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

    clawA.physicsBody.affectedByGravity = NO;

    clawA.physicsBody.angularDamping = 1.0;

    

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

    clawB.physicsBody.affectedByGravity = NO;

    clawB.physicsBody.angularDamping = 1.0;

    

    SKPhysicsJointLimit *limit = [SKPhysicsJointLimit jointWithBodyA:clawA.physicsBody bodyB:clawB.physicsBody anchorA:clawA.position anchorB:clawB.position];

    limit.maxLength = 70;

    [self.physicsWorld addJoint:limit];

}

– (void)update:(NSTimeInterval)currentTime

{

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

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

    if (self.direction == 0 && clawA.physicsBody.velocity.dy < 0.1 && clawA.position.y > 60) {

        [clawA.physicsBody applyForce:CGVectorMake(0, –0.1)];

    }

    

    if (clawA.position.y < 60 && self.direction == 0) {

        clawA.physicsBody.velocity = CGVectorMake(0, 0);

        self.direction = 1;

    }

    

    if (self.direction == 1) {

        clawA.physicsBody.velocity = CGVectorMake(0, 15);

        [self hold];

    }

    

    if (self.direction == 1 && clawA.position.y > CGRectGetMaxY(self.frame) – 80) {

        self.direction = 2;

    }

    

    if (self.direction == 2) {

        clawA.position = CGPointMake(clawA.position.x, CGRectGetMaxY(self.frame) – 80);

        clawA.physicsBody.velocity = CGVectorMake(15, 0);

        [self hold];

    }

    

    float x = clawB.position.x – clawA.position.x;

    clawB.position = CGPointMake(clawA.position.x + x, clawA.position.y);

}

– (void)hold

{

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

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

    if (clawA.zRotation < M_PI * 0.15 && clawA.zRotation > –0.15) {

        [clawA.physicsBody applyTorque:0.01];

    } else {

        clawA.physicsBody.angularVelocity = 0;

    }

    

    if (clawB.zRotation > – M_PI * 0.2 && clawB.zRotation < –0.1) {

        [clawB.physicsBody applyTorque:-0.01];

    } else {

        clawB.physicsBody.angularVelocity = 0;

    }

}

– (void)didSimulatePhysics

{

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

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

    

    float x = (clawA.position.x + clawB.position.x) / 2.0;

    float y = clawA.position.y + 30;

    if (!self.wire) {

        self.wire = [SKShapeNode node];

        self.wire.strokeColor = [SKColor darkGrayColor];

        [self addChild:self.wire];

    }

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:clawA.position];

    [path addLineToPoint:CGPointMake(x, y)];

    [path addLineToPoint:clawB.position];

    [path moveToPoint:CGPointMake(x, y)];

    [path addLineToPoint:CGPointMake(x, CGRectGetMaxY(self.frame))];

    self.wire.path = path.CGPath;

}

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

{

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

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

    [clawA removeFromParent];

    [clawB removeFromParent];

    

    self.direction = 0;

    [self createShachihoko];

    [self createClaw];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidAppear:(BOOL)animated

{

    [super viewDidAppear:animated];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end