iPhone 車ゲーム シンプル

ハンドルをタップして車を運転。ゴールフラッグ指してドライブという感じのiPhoneゲームのサンプルコードを描いてみます。

使った画像

サンプルを動かした動画

サンプルコード

#import “ViewController.h”

#import <SpriteKit/SpriteKit.h>

typedef enum : uint8_t {

    ColliderTypeWall = 0x1 << 1,

    ColliderTypeCar = 0x1 << 2,

    ColliderTypeFlag = 0x1 << 3,

    ColliderTypeGetFlag = 0x1 << 4,

} MyColliderType;

@interface RaceScene : SKScene <SKPhysicsContactDelegate>

@property BOOL contentCreated;

@end

@implementation RaceScene

– (void)didMoveToView:(SKView *)view

{

    if (!self.contentCreated) {

        [self createSceneContents];

        self.contentCreated = YES;

        self.backgroundColor = [SKColor lightGrayColor];

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

        self.physicsWorld.contactDelegate = self;

    }

}

– (void)createSceneContents

{

    [self createCar];

    [self createWall];

    [self createFlag];

    [self createStaring];

}

– (void)createCar

{

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

    car.name = @”car”;

    car.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    [self addChild:car];

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

    car.physicsBody.categoryBitMask = ColliderTypeCar;

    car.physicsBody.contactTestBitMask = ColliderTypeFlag;

}

– (void)createWall

{

    CGRect frames[4] = {CGRectMake(20, 120, 280, 20), CGRectMake(280, 120, 20, 280), CGRectMake(20, 400, 280, 20), CGRectMake(20, 120, 20, 280)};

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

        SKSpriteNode *wall = [SKSpriteNode spriteNodeWithColor:[SKColor brownColor] size:frames[i].size];

        wall.position = CGPointMake(CGRectGetMidX(frames[i]), CGRectGetMidY(frames[i]));

        [self addChild:wall];

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

        wall.physicsBody.dynamic = NO;

        wall.physicsBody.categoryBitMask = ColliderTypeWall;

    }

}

– (void)createFlag

{

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

    float x = arc4random() % 200 + 50;

    float y = arc4random() % 200 + 200;

    flag.size = CGSizeMake(40, 40);

    flag.position = CGPointMake(x, y);

    [self addChild:flag];

    

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

    flag.physicsBody.dynamic = NO;

    flag.physicsBody.categoryBitMask = ColliderTypeFlag;

    flag.physicsBody.collisionBitMask = ColliderTypeCar;

}

– (void)createStaring

{

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

    staring.name = @”staring”;

    staring.position = CGPointMake(CGRectGetMidX(self.frame), 50);

    [self addChild:staring];

}

– (void)update:(NSTimeInterval)currentTime

{

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

    float dx = 20.0 * cos(car.zRotation + M_PI);

    float dy = 20.0 * sin(car.zRotation + M_PI);

    car.physicsBody.velocity = CGVectorMake(dx, dy);

}

– (void)didBeginContact:(SKPhysicsContact *)contact

{

    SKNode *car;

    SKNode *flag;

    if (contact.bodyA.categoryBitMask == ColliderTypeCar) {

        car = contact.bodyA.node;

        flag = contact.bodyB.node;

    } else {

        car = contact.bodyB.node;

        flag = contact.bodyA.node;

    }

    

    SKAction *turn = [SKAction rotateByAngle:2.0 * M_PI duration:0.1];

    [car runAction:turn];

    

    flag.physicsBody.categoryBitMask = ColliderTypeGetFlag;

    SKAction *zoom = [SKAction scaleTo:2.0 duration:0.3];

    SKAction *fade = [SKAction fadeAlphaTo:0.3 duration:0.3];

    [flag runAction:[SKAction group:@[zoom, fade]] completion:^{

        [flag removeFromParent];

    }];

    

    [self createFlag];

}

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

{

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

    

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

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

    if (CGRectContainsPoint(staring.frame, p)) {

        SKAction *turnA = [SKAction rotateByAngle:M_PI * 0.5 duration:0.2];

        SKAction *turnB = [SKAction rotateToAngle:0 duration:0.5];

        [staring runAction:[SKAction sequence:@[turnA, turnB]]];

        [car runAction:turnA];

    }

}

@end

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

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

    [self.view addSubview:spriteView];

    

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

    [spriteView presentScene:scene];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end