iPhoneイカ

ゲートにイカを通して先に進んでいくようなiPhoneアプリのサンプルコードを描いてみます。

動かすとこんな感じです

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface SquidScene : SKScene

@end

@implementation SquidScene

– (void)didMoveToView:(SKView *)view

{

    [self createSceneContents];

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor colorWithRed:0 green:0 blue:0.2 alpha:1];

    [self createSquid];

    [self createGate:0];

    self.physicsWorld.speed = 0;

}

– (void)createSquid

{

    SKNode *squid = [[SKNode alloc] init];

    squid.name = @”squid”;

    squid.position = CGPointMake(160, 100);

    [self addChild:squid];

    

    UIBezierPath *headPath = [UIBezierPath bezierPath];

    [headPath moveToPoint:CGPointMake(0, 80)];

    [headPath addLineToPoint:CGPointMake(20, 50)];

    [headPath addLineToPoint:CGPointMake(-20, 50)];

    [headPath closePath];

    SKShapeNode *head = [SKShapeNode node];

    head.path = headPath.CGPath;

    [squid addChild:head];

    

    UIBezierPath *bodyPath = [UIBezierPath bezierPath];

    [bodyPath moveToPoint:CGPointMake(0, 80)];

    [bodyPath addLineToPoint:CGPointMake(20, 20)];

    [bodyPath addLineToPoint:CGPointMake(-20, 20)];

    [bodyPath closePath];

    SKShapeNode *body = [SKShapeNode node];

    body.path = bodyPath.CGPath;

    [squid addChild:body];

    

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

        float x = i * 514;

        float y = 10;

        SKSpriteNode *leg = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(1, 20)];

        leg.position = CGPointMake(x, y);

        [squid addChild:leg];

    }

    

    UIBezierPath *ePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-3, –3, 6, 6)];

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

        float x = 20 * i – 10;

        SKShapeNode *eye = [SKShapeNode node];

        eye.path = ePath.CGPath;

        eye.position = CGPointMake(x, 20);

        eye.fillColor = [SKColor whiteColor];

        [squid addChild:eye];

    }

    

    SKPhysicsBody *hBody = [SKPhysicsBody bodyWithPolygonFromPath:headPath.CGPath];

    SKPhysicsBody *bBody = [SKPhysicsBody bodyWithPolygonFromPath:bodyPath.CGPath];

    squid.physicsBody = [SKPhysicsBody bodyWithBodies:@[hBody, bBody]];

    squid.physicsBody.linearDamping = 1.0;

    squid.physicsBody.friction = 1.0;

    squid.physicsBody.allowsRotation = NO;

}

– (void)createGate:(float)x

{

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

        SKSpriteNode *wall = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(120, 2)];

        wall.name = [NSString stringWithFormat:@”wall%d”, i];

        wall.position = CGPointMake(70 + i*180 + x, 400);

        [self addChild:wall];

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

    }

}

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

{

    if (self.physicsWorld.speed == 0) {

        self.physicsWorld.speed = 0.1;

        return;

    }

    

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

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

    

    float dx = (p.x – squid.position.x) / 10.0;

    [squid.physicsBody applyImpulse:CGVectorMake(dx, 10)];

}

– (void)didSimulatePhysics

{

    SKNode *wall0 = [self childNodeWithName:@”wall0″];

    if (wall0.position.y < 0) {

        [wall0 removeFromParent];

        [[self childNodeWithName:@”wall1″] removeFromParent];

        

        float rand = arc4random() % 100;

        [self createGate:rand – 50];

    }

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

@end