iPhoneおむすびころりん

おむすびを転がして、穴に落ちたら小判がぴょこっと出る。というかんじでiPhoneアプリのサンプルコードを描いてみます。

今回使った画像


サンプルを動画でみると

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface KororinScene : SKScene

@property BOOL contentCreated;

@end

@implementation KororinScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor greenColor];

    [self createOmusubiAtPoint:CGPointMake(80, CGRectGetMaxY(self.frame))];

    [self createHillSlope];

    [self createHole];

}

– (void)createOmusubiAtPoint:(CGPoint)p

{

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

    omusubi.name = @”omusubi”;

    omusubi.position = p;

    [self addChild:omusubi];

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path moveToPoint:CGPointMake(-25, –25)];

    [path addLineToPoint:CGPointMake(0, 25)];

    [path addLineToPoint:CGPointMake(25, –25)];

    omusubi.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path.CGPath];

    omusubi.physicsBody.friction = 1.0;

    omusubi.physicsBody.restitution = 0.7;

}

– (void)createHillSlope

{

    // sin curve

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

        SKSpriteNode *block = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:CGSizeMake(10, 10)];

        float x = 10 * i;

        float y = 120 * sin(M_PI / 40.0 * (float)i + M_PI/2.0) + 120;

        block.position = CGPointMake(x, y);

        [self addChild:block];

        

        block.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];

        block.physicsBody.friction = 0.5;

        block.physicsBody.dynamic = NO;

    }

}

– (void)createHole

{

    SKSpriteNode *hole = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(40, 30)];

    hole.position = CGPointMake(CGRectGetMaxX(self.frame) – 60, –10);

    hole.name = @”hole”;

    [self addChild:hole];

}

– (void)showKoban

{

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

    koban.position = CGPointMake(CGRectGetMaxX(self.frame) – 60, –50);

    [self addChild:koban];

    

    SKAction *up = [SKAction moveByX:0 y:80 duration:0.5];

    SKAction *fade = [SKAction fadeAlphaTo:0 duration:1.0];

    [koban runAction:[SKAction sequence:@[up, fade]] completion:^{

        [koban removeFromParent];

    }];

    

}

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

{

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

    [self createOmusubiAtPoint:p];

}

– (void)didSimulatePhysics

{

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

        

        if (omusubi.position.y < –20) {

            

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

            if ([hole intersectsNode:omusubi]) {

                [self showKoban];

            }

            [omusubi removeFromParent];

        }

        

        if (omusubi.physicsBody.resting) {

            SKAction *fade = [SKAction fadeAlphaTo:0 duration:0.5];

            [omusubi runAction:fade completion:^{

                [omusubi removeFromParent];

            }];

        }

    }];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    SKView *spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxY(self.view.frame), CGRectGetMaxX(self.view.frame))];

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end