iPhoneかえるパラシュート

カエルがパラシュートで降下してくるシンプルなiPhoneアプリゲームのサンプルコードを描いてみます。


動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface FallFrog : SKScene <SKPhysicsContactDelegate>

@property BOOL contentCreated;

@end

typedef enum {

    ObjectCategoryFrog = 1,

    ObjectCategoryBom = 2,

} ObjectCategory;

@implementation FallFrog

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

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

    self.physicsWorld.gravity = CGVectorMake(0, –0.5);

    self.physicsWorld.contactDelegate = self;

    [self createFrog];

    [self createRandomBom];

}

– (void)createFrog

{

    SKSpriteNode *frog = [SKSpriteNode spriteNodeWithImageNamed:@”fallfrog_frog”];

    frog.name = @”frog”;

    frog.position = CGPointMake(160, CGRectGetHeight(self.view.frame));

    [self addChild:frog];

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

    frog.physicsBody.categoryBitMask = ObjectCategoryFrog;

    frog.physicsBody.contactTestBitMask = ObjectCategoryBom;

    

    SKSpriteNode *parachute = [SKSpriteNode spriteNodeWithImageNamed:@”fallfrog_para”];

    parachute.name = @”para”;

    parachute.position = CGPointMake(0, 55);

    [frog addChild:parachute];

}

– (void)createRandomBom

{

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

        float x = arc4random() % 200 + 50;

        SKSpriteNode *bom = [SKSpriteNode spriteNodeWithImageNamed:@”fallfrog_bom”];

        bom.position = CGPointMake(x, 80 * i + 100);

        [self addChild:bom];

        

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

        bom.physicsBody.dynamic = NO;

        bom.physicsBody.categoryBitMask = ObjectCategoryBom;

    }

}

– (void)showSparkWithPoint:(CGPoint)p

{

    NSString *path = [[NSBundle mainBundle] pathForResource:@”spark” ofType:@”sks”];

    SKEmitterNode *emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    emitter.name = @”spark”;

    emitter.particlePosition = p;

    [self addChild:emitter];

    [emitter runAction:[SKAction sequence:@[[SKAction fadeAlphaTo:0 duration:0.6], [SKAction removeFromParent]]]];

}

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

{

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

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

    if (p.x > frog.position.x) {

        [frog.physicsBody applyImpulse:CGVectorMake(10, 0)];

    } else if (p.x < frog.position.x) {

        [frog.physicsBody applyImpulse:CGVectorMake(-10, 0)];

    }

}

– (void)didSimulatePhysics

{

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

    SKNode *para = [frog childNodeWithName:@”para”];

    para.zRotation = frog.physicsBody.velocity.dx / 1000.0;

    

    if (frog.position.y < – 100) {

        [self restart];

    }

}

– (void)didBeginContact:(SKPhysicsContact *)contact

{

    SKNode *frog;

    SKNode *bom;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {

        frog = contact.bodyA.node;

        bom = contact.bodyB.node;

    } else {

        frog = contact.bodyB.node;

        bom = contact.bodyA.node;

    }

    

    [bom removeFromParent];

    [self showSparkWithPoint:bom.position];

    

    // remove parachute and fall speed up

    [[frog childNodeWithName:@”para”] removeFromParent];

    self.physicsWorld.gravity = CGVectorMake(0, –2.0);

}

– (void)restart

{

    [[self children] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        [obj removeFromParent];

    }];

    

    self.physicsWorld.gravity = CGVectorMake(0, –0.5);

    [self createFrog];

    [self createRandomBom];

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end