iPhoneパラシュートゲーム

落ちてくる茶色の箱をタッチしたらパラシュートをひらく。ただそれだけの超簡単iPhoneゲームのサンプルを作ってみます。

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

ポイント
箱が落ちてくるアニメーションは、CADisplayLinkを使ってLoopの中で箱のcenter座標を更新しています。箱の落ちるスピードを変数で持っておいて、タップした際にその値に変更を加えることで、Loop内部の座標更新のX,Y方向の大きさをコントロールしました。

サンプルコード

#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 () {

    CADisplayLink *timer;

    UIView *parachute;

    CGPoint speed;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [self color:3];

    [self createStripe];

    [self startTimer];

}

– (void)startTimer

{

    timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];

    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

}

– (void)tick:(CADisplayLink*)sender

{

    if (parachute) {

        parachute.center = CGPointMake(parachute.center.x + speed.x, parachute.center.y + speed.y);

        if (!CGRectIntersectsRect(parachute.frame, self.view.bounds)) {

            [parachute removeFromSuperview];

            parachute = 0;

        }

    } else {

        [self createParachute];

    }

}

– (void)createParachute

{

    parachute = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 50, 50)];

    parachute.backgroundColor = [self color:1];

    parachute.layer.cornerRadius = 5;

    [self.view addSubview:parachute];

    

    speed = CGPointMake(0, 3);

    

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

    [parachute addGestureRecognizer:tap];

}

– (void)openParachute:(UITapGestureRecognizer*)gr

{

    speed = CGPointMake(1.5, 1.5);

    

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path addArcWithCenter:CGPointMake(25, –50) radius:50 startAngle:0 endAngle:M_PI clockwise:NO];

    CAShapeLayer *sl = [[CAShapeLayer alloc] init];

    sl.fillColor = [self color:0].CGColor;

    sl.path = path.CGPath;

    

    

    UIBezierPath *path2 = [UIBezierPath bezierPath];

    [path2 moveToPoint:CGPointMake(-25, –50)];

    [path2 addLineToPoint:CGPointMake(25, 0)];

    [path2 moveToPoint:CGPointMake(0, –50)];

    [path2 addLineToPoint:CGPointMake(25, 0)];

    [path2 moveToPoint:CGPointMake(30, –50)];

    [path2 addLineToPoint:CGPointMake(25, 0)];

    [path2 moveToPoint:CGPointMake(75, –50)];

    [path2 addLineToPoint:CGPointMake(25, 0)];

    

    CAShapeLayer *sl2 = [[CAShapeLayer alloc] init];

    sl2.fillColor = [UIColor clearColor].CGColor;

    sl2.strokeColor = [self color:4].CGColor;

    sl2.lineWidth = 2;

    sl2.path = path2.CGPath;

    

    [parachute.layer addSublayer:sl];

    [parachute.layer addSublayer:sl2];

}

– (void)createStripe

{

    UIBezierPath *path = [UIBezierPath bezierPath];

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

        [path moveToPoint:CGPointMake(20, 100 * i)];

        [path addLineToPoint:CGPointMake(300, 100 * (i + 3))];

    }

    

    CAShapeLayer *sl = [[CAShapeLayer alloc] init];

    sl.fillColor = [UIColor clearColor].CGColor;

    sl.strokeColor = [self color:2].CGColor;

    sl.lineWidth = 4;

    sl.path = path.CGPath;

    

    [self.view.layer addSublayer:sl];

}

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0x70FF2E);

        case 1:

            return UIColorHex(0xE8B129);

        case 2:

            return UIColorHex(0xFDFDFD);

        case 3:

            return UIColorHex(0x6934E8);

        case 4:

            return UIColorHex(0x0E061A);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end