iPhoneフォークリフトゲーム

フォークリフトで棚に荷物を運んでいく簡単なiPhoneゲームアプリを作ってみます。左右に進むボタンと、画面左に高さの違う棚を用意します。棚をタップするとその高さに合わせて、フォークリフトの荷物の高さを調整、棚に荷物を降ろすようにしてみました。


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

ポイント
左右の動きはL、Rと表示したUILabelに UILongPressGestureRecognizerを設定して、押してる間はフォークリフトが左右に動き続けるようにNSTimerをかけています。フォークの高さは、画面左の四角いUIViewにUITapGestureを設定して、タップでおなじ高さまでアニメーションするようにしました。

サンプルコード

#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 *forkLift;

    UIView *fork;

    UIView *box;

    NSTimer *timer;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

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

    

    [self createStore];

    

    [self createForkLift];

    

    [self createButton];

    

    [self createBox];

}

– (void)createForkLift

{

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

    forkLift.center = CGPointMake(260, 400);

    forkLift.layer.borderColor = [self color:4].CGColor;

    [self.view addSubview:forkLift];

    

    UIView *roof = [[UIView alloc] initWithFrame:CGRectMake(40, 10, 40, 40)];

    roof.backgroundColor = [UIColor clearColor];

    roof.layer.borderColor = [self color:4].CGColor;

    roof.layer.borderWidth = 5;

    roof.layer.cornerRadius = 5;

    [forkLift addSubview:roof];

    

    

    UIView *body = [[UIView alloc] initWithFrame:CGRectMake(20, 40, 60, 40)];

    body.backgroundColor = [self color:3];

    [forkLift addSubview:body];

    

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

        UIView *tire = [[UIView alloc] initWithFrame:CGRectMake(20 + 40 * i, 70, 20, 20)];

        tire.backgroundColor = [UIColor whiteColor];

        tire.layer.borderColor = [self color:4].CGColor;

        tire.layer.borderWidth = 5;

        tire.layer.cornerRadius = 10;

        [forkLift addSubview:tire];

    }

    

    UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(20, –200, 5, 260)];

    bar.backgroundColor = [self color:4];

    [forkLift addSubview:bar];

    

    fork = [[UIView alloc] initWithFrame:CGRectMake(-20, 40, 40, 5)];

    fork.backgroundColor = [self color:4];

    [forkLift addSubview:fork];

}

– (void)createButton

{

    UILabel *button = [[UILabel alloc] initWithFrame:CGRectMake(50, 470, 50, 50)];

    button.text = @”L”;

    button.textAlignment = 1;

    button.font = [UIFont boldSystemFontOfSize:50];

    button.textColor = [UIColor whiteColor];

    button.backgroundColor = [self color:4];

    [self.view addSubview:button];

    

    UILabel *buttonR = [[UILabel alloc] initWithFrame:CGRectMake(220, 470, 50, 50)];

    buttonR.text = @”R”;

    buttonR.textAlignment = 1;

    buttonR.font = [UIFont boldSystemFontOfSize:50];

    buttonR.textColor = [UIColor whiteColor];

    buttonR.backgroundColor = [self color:4];

    [self.view addSubview:buttonR];

    

    for (UIView *v in @[button, buttonR]) {

        v.userInteractionEnabled = YES;

        UILongPressGestureRecognizer *tap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];

        [v addGestureRecognizer:tap];

    }

}

– (void)createBox

{

    box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    box.backgroundColor = [self color:2];

    [forkLift addSubview:box];

    

    box.center = CGPointMake(fork.center.x2, fork.center.y25);

}

– (void)createStore

{

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

        UIView *storeBox = [[UIView alloc] initWithFrame:CGRectMake(0, 60 * i + 150, 60, 50)];

        storeBox.backgroundColor = [self color:0];

        [self.view addSubview:storeBox];

        

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

        [storeBox addGestureRecognizer:tap];

    }

}

– (void)move:(UILongPressGestureRecognizer*)gr

{

    UILabel *l = (UILabel*)gr.view;

    

    if (gr.state == UIGestureRecognizerStateBegan) {

        if ([l.text isEqual:@”L”]) {

            timer = [NSTimer scheduledTimerWithTimeInterval:1.0/15.0 target:self selector:@selector(moveLeft:) userInfo:nil repeats:YES];

        }

        else

        {

            timer = [NSTimer scheduledTimerWithTimeInterval:1.0/15.0 target:self selector:@selector(moveRight:) userInfo:nil repeats:YES];

        }

    }

    else

    {

        [timer invalidate];

    }

}

– (void)moveLeft:(NSTimer *)sender

{

    forkLift.center = CGPointMake(forkLift.center.x2, forkLift.center.y);

    if (forkLift.center.x < 100) {

        [sender invalidate];

    }

}

– (void)moveRight:(NSTimer *)sender

{

    forkLift.center = CGPointMake(forkLift.center.x + 2, forkLift.center.y);

    if (forkLift.center.x > 270) {

        [sender invalidate];

    }

}

– (void)updown:(UITapGestureRecognizer*)gr

{

    CGPoint p = [forkLift convertPoint:gr.view.center fromView:self.view];

    [UIView animateWithDuration:1.0 animations:^{

        fork.center = CGPointMake(fork.center.x, p.y + 25);

        box.center = CGPointMake(box.center.x, fork.center.y25);

    } completion:^(BOOL finished) {

        // check

        CGRect boxInGlobal = [self.view convertRect:box.frame fromView:forkLift];

        if (CGRectIntersectsRect(boxInGlobal, gr.view.frame)) {

            [UIView animateWithDuration:0.5 animations:^{

                box.center = [self.view convertPoint:gr.view.center toView:forkLift];

            } completion:^(BOOL finished) {

                // convert box

                box.center = [self.view convertPoint:box.center fromView:forkLift];

                [self.view addSubview:box];

                [self reloadBox];

            }];

        }

    }];

}

– (void)reloadBox

{

    [UIView animateWithDuration:0.5 animations:^{

        forkLift.center = CGPointMake(260, 400);

        fork.center = CGPointMake(fork.center.x, 40);

    } completion:^(BOOL finished) {

        [self createBox];

    }];

}

– (UIColor*)color:(int)i

{

    switch (i) {

        case 0:

            return UIColorHex(0xFFF0D5);

        case 1:

            return UIColorHex(0xEAD3AA);

        case 2:

            return UIColorHex(0xA39075);

        case 3:

            return UIColorHex(0x705F4D);

        case 4:

            return UIColorHex(0x231F1C);

        default:

            break;

    }

    return nil;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end