iPhoneハードルジャンプ

ハードルをジャンプしてとびこえていくシンプルゲームのiPhoneアプリを描いてみます。

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

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

@interface HurdleScene : SKScene

@property BOOL contentCreated;

@property BOOL jumping;

@end

@implementation HurdleScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

    }

}

– (void)createSceneContents

{

    self.backgroundColor = [SKColor brownColor];

    

    SKSpriteNode *ground = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(568, 10)];

    ground.position = CGPointMake(284, 20);

    [self addChild:ground];

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

    ground.physicsBody.dynamic = NO;

    

    [self createMan];

    

    [self createHurdle];

}

– (void)createMan

{

    SKTexture *walkA = [SKTexture textureWithImageNamed:@”hurdle_man1″];

    SKSpriteNode *man = [SKSpriteNode spriteNodeWithTexture:walkA];

    man.name = @”man”;

    man.position = CGPointMake(260, 100);

    [self addChild:man];

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

    man.physicsBody.allowsRotation = NO;

    [self running];

}

– (void)running

{

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

    NSArray *textures = @[[SKTexture textureWithImageNamed:@”hurdle_man1″],

                          [SKTexture textureWithImageNamed:@”hurdle_man2″]];

    SKAction *running = [SKAction animateWithTextures:textures timePerFrame:0.2 resize:YES restore:NO];

    [man runAction:[SKAction repeatActionForever:running]];

}

– (void)jump

{

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

    NSArray *textures = @[[SKTexture textureWithImageNamed:@”hurdle_man1″],

                          [SKTexture textureWithImageNamed:@”hurdle_man2″]];

    SKAction *running = [SKAction animateWithTextures:textures timePerFrame:0.2 resize:YES restore:NO];

    [man runAction:[SKAction repeatActionForever:running]];

}

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

{

    if (self.jumping == YES) {

        return;

    }

    

    self.jumping = YES;

    

    SKSpriteNode *man = (SKSpriteNode*)[self childNodeWithName:@”man”];

    [man removeAllActions];

    

    SKAction *setTexture = [SKAction animateWithTextures:@[[SKTexture textureWithImageNamed:@”hurdle_man3″]] timePerFrame:0 resize:YES restore:NO];

    SKAction *jump = [SKAction runBlock:^{

        [man.physicsBody applyImpulse:CGVectorMake(0, 220)];

    }];

    SKAction *waiting = [SKAction waitForDuration:0.8];

    SKAction *run = [SKAction runBlock:^{

        self.jumping = NO;

        [self running];

    }];

    [man runAction:[SKAction sequence:@[setTexture, jump, waiting, run]]];

}

– (void)createHurdle

{

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

    hurdle.name = @”hurdle”;

    [self addChild:hurdle];

    hurdle.position = CGPointMake(560, 60);

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

    hurdle.physicsBody.friction = 0;

}

– (void)didSimulatePhysics

{

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

    if (hurdle.position.x < – 20) {

        [hurdle removeFromParent];

        [self createHurdle];

    } else {

        hurdle.physicsBody.velocity = CGVectorMake(-250, 0);

    }

    

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

    if (man.position.x < – 50) {

        [man removeFromParent];

        [self createMan];

    }

}

@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 = [[HurdleScene alloc] initWithSize:spriteView.frame.size];

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end