簡単3Dシューティングにチャレンジ
じわじわせまってくる敵をタップして
あっちに行ってもらおう!
という感じで、子供向けiPhoneゲームのサンプルコードを書いてみた

ポイント
CATransform3Dのm34パラメータを使って、3Dっぽく奥行きを出しています。
的と地面の中心Y座標を揃えることで、地面の上を動いているように見せています。
(x座標は適当に補正しました。)

環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。

iPhone 簡単3Dシューティング サンプルコード

サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

#define UIColorHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface ViewController() {

    UIView *road;

    CATransform3D baseTrans;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.view.backgroundColor = [self colorAtIndex:0];

    

    [self createGround];

    

    [self createTargets];

    

    [self start];

}

– (void)createGround

{

    road = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 220, 1000)];

    road.center = self.view.center;

    

    CATransform3D transform = CATransform3DIdentity;

    transform.m34 = 1.0 / – 500.0;

    baseTrans =  CATransform3DRotate(transform, 0.45 * M_PI, 1.0f, 0.0f, 0.0f);

    road.layer.transform = baseTrans;

    road.backgroundColor = [self colorAtIndex:1];

    [self.view addSubview:road];

}

– (void)createTargets

{

    

    int x = (arc4random()%3) * 40 + 100;

    UIView *target = [[UIView alloc] initWithFrame:CGRectMake(x, road.center.y, 50, –50)];

    target.layer.cornerRadius = 5.0;

    

    UIView *eyeR = [[UIView alloc] initWithFrame:CGRectMake(5, –15, 10, 10)];

    eyeR.backgroundColor = [UIColor whiteColor];

    eyeR.layer.cornerRadius = 5;

    [target addSubview:eyeR];

    

    

    UIView *eyeL = [[UIView alloc] initWithFrame:CGRectMake(35, –15, 10, 10)];

    eyeL.backgroundColor = [UIColor whiteColor];

    eyeL.layer.cornerRadius = 5;

    [target addSubview:eyeL];

    

    target.backgroundColor = [self colorAtIndex:3];

    CATransform3D t = baseTrans;

    t = CATransform3DRotate(t, M_PI * 0.5, 1, 0, 0);

    target.layer.transform = t;

    [self.view addSubview:target];

    

    [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(move:) userInfo:target repeats:YES];

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hit:)];

    [target addGestureRecognizer:tap];

}

– (void)move:(NSTimer*)sender

{

    UIView *target = sender.userInfo;

    target.layer.transform = CATransform3DTranslate(target.layer.transform, 0, 0, –1);

    

    // rough estimateroughly… adjust

    float dx = 0.5 * (target.center.x160.0) / 160.0;

    target.center = CGPointMake(target.center.x + dx, target.center.y);

}

– (UIColor*)colorAtIndex:(int)index

{

    // 001E4E 006AC1 001940 2C4566

    switch (index) {

        case 0:

            return UIColorHex(0x001E4E);

            break;

        case 1:

            return UIColorHex(0x006AC1);

            break;

        case 2:

            return UIColorHex(0x001940);

            break;

        case 3:

            return UIColorHex(0x2C4566);

            break;

        default:

            break;

    }

    return nil;

}

– (void)start

{

    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(createTargets) userInfo:nil repeats:YES];

}

– (void)hit:(UITapGestureRecognizer*)gr

{

    gr.view.backgroundColor = [self colorAtIndex:2];

    

    [UIView animateWithDuration:0.2 animations:^{

        gr.view.center = CGPointMake(gr.view.center.x + 400, gr.view.center.y);

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end