iPhone棒飛び

足下に進んでくる棒(黒い四角)をジャンプでとびこえて遊ぶシンプルなゲームをiPhoneアプリとして描いてみます。(SpriteKit使ってます。)


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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface PlayGround : SKScene

@property BOOL contentCreated;

@property SKSpriteNode *bar;

@end

@implementation PlayGround

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.scene.backgroundColor = [SKColor lightGrayColor];

    

    [self createGround];

    [self createBar];

    

    [self createJumper];

    

}

– (void)createGround

{

    SKSpriteNode *ground = [[SKSpriteNode alloc] initWithColor:[SKColor blackColor] size:CGSizeMake(568, 20)];

    ground.position = CGPointMake(284, 0);

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

    ground.physicsBody.dynamic = NO;

    ground.physicsBody.friction = 0;

    [self addChild:ground];

}

– (void)createBar

{

    self.bar = [[SKSpriteNode alloc] initWithColor:[SKColor blackColor] size:CGSizeMake(20, 20)];

    self.bar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.bar.size];

    self.bar.position = CGPointMake(20, 20);

    [self addChild:self.bar];

    

    self.bar.physicsBody.friction = 0.0001;

}

– (void)createJumper

{

    CGPoint points[] = {CGPointMake(120, 140), CGPointMake(180, 40), CGPointMake(250, 40), CGPointMake(350, 40)};

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

        SKSpriteNode *jumper = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 60)];

        jumper.position = points[i];

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

        [self addChild:jumper];

        jumper.name = @”jumper”;

    }

}

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

{

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

    

    for (SKNode *node in self.children) {

        if ([node.name isEqual:@”jumper”]) {

            if ([node containsPoint:touchPoint]) {

                [node.physicsBody applyImpulse:CGVectorMake(0, 30)];

            }

        }

    }

}

– (void)didSimulatePhysics

{

    [self.bar.physicsBody applyImpulse:CGVectorMake(0.03, 0)];

    

    if (self.bar.position.x > 548) {

        self.bar.position = CGPointMake(0, self.bar.position.y);

    }

    

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

    if (!jumper) {

        [self createJumper];

    } else {

        if (jumper.position.y < 0) {

            [jumper removeFromParent];

        }

    }

}

@end

@interface ViewController ()

@property (strong, nonatomic) UIView *bar;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    SKView *spriteView = [[SKView alloc] initWithFrame:CGRectMake(0, 0, 568, 320)];

    [self.view addSubview:spriteView];

    

    SKScene *scene = [[PlayGround alloc] initWithSize:CGSizeMake(568, 320)];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end