iPhoneゲーム マンション脱出

5つのドアのうち、一つだけ下の階に繋がるドアがあります。正解のドアをみつけて、ドンドン下に逃げていきましょう。という感じのiPhoneアプリの作成方法を書いていきます。


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

ポイント
ドアは、UIViewで描いています。枠はlayerのborder、ドアノブはlayerのcornerRadiusで丸くしてという感じです。人の画像は、pict.pngという名前で画像ファイルを適当に用意しました。5枚のドアを一つの単位にしています。表示の際に、ランダムでひとつ正解のドアを決めておいて、UIViewのtag == 1として判定できるようにしました。

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIImageView *player;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    

    [self createPlayer];

    

    [self lineUpDoors];

}

– (void)createPlayer

{

    UIImage *image = [UIImage imageNamed:@”pict”];

    player = [[UIImageView alloc] initWithImage:image];

    player.contentMode = UIViewContentModeScaleToFill;

    player.frame = CGRectMake(0, 330, 40, 50);

    [self.view addSubview:player];

}

– (void)lineUpDoors

{

    int correct = arc4random() % 5;

    

    float w = 320.0 / 6.0;

    

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

        float x = (w + 6) * i + 15;

        float y = 300;

        UIView *door = [self createDoor:CGRectMake(x, y, w, w * 1.5)];

        [self.view addSubview:door];

        

        if (i == correct) {

            door.tag = 1;

        }

    }

    

    UIView *floor = [[UIView alloc] initWithFrame:CGRectMake(0, 300 + w * 1.4, 320, 8)];

    floor.backgroundColor = [UIColor blackColor];

    [self.view addSubview:floor];

    

    // player 

    [self.view addSubview:player];

}

– (UIView*)createDoor:(CGRect)frame

{

    UIView *doorFrame = [[UIView alloc] initWithFrame:frame];

    doorFrame.layer.borderColor = [UIColor blackColor].CGColor;

    doorFrame.layer.borderWidth = 5;

    [self.view addSubview:doorFrame];

    

    UIView *door = [[UIView alloc] initWithFrame:CGRectMake(7, 7, doorFrame.bounds.size.width14, doorFrame.bounds.size.height14)];

    door.backgroundColor = [UIColor blackColor];

    door.layer.anchorPoint = CGPointMake(1.0, 0.5);

    door.layer.position = CGPointMake(door.bounds.size.width + 7, door.bounds.size.height * 0.5 + 7);

    [doorFrame addSubview:door];

    

    UIView *knob = [[UIView alloc] initWithFrame:CGRectMake(5, door.bounds.size.height * 0.5, 6, 6)];

    knob.layer.cornerRadius = 3;

    knob.backgroundColor = [UIColor whiteColor];

    [door addSubview:knob];

    

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

    [door addGestureRecognizer:tap];

    

    return doorFrame;

}

– (void)open:(UITapGestureRecognizer*)gr

{

    CGPoint p = gr.view.superview.center;

    [UIView animateWithDuration:1.0 animations:^{

        player.center = CGPointMake(p.x, p.y + 15);

        gr.view.layer.transform = CATransform3DMakeRotation(M_PI * 0.4, 0, 1, 0);

    } completion:^(BOOL finished) {

        if (gr.view.superview.tag == 1) {

            // correct!

            [gr.view.superview bringSubviewToFront:player];

            [UIView animateWithDuration:0.5 animations:^{

                CGAffineTransform t = CGAffineTransformMakeScale(0.8, 0.8);

                t = CGAffineTransformTranslate(t, –5, –5);

                player.transform = t;

                player.alpha = 0.5;

            } completion:^(BOOL finished) {

                // next door

                [player removeFromSuperview];

                

                

                [UIView animateWithDuration:0.5 animations:^{

                    for (UIView *v in self.view.subviews) {

                        v.userInteractionEnabled = NO;

                        v.center = CGPointMake(v.center.x, v.center.y150);

                    }

                } completion:^(BOOL finished) {

                    [self createPlayer];

                    [self lineUpDoors];

                }];

            }];

        }

    }];

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end