iPhoneシンプルシューティング

飛行機をタップすると、弾を発射するかんじのiPhoneアプリを描いてみます。


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

サンプルコード

#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@property (strong, nonatomic) NSMutableArray *walls;

@property (strong, nonatomic) NSMutableArray *balls;

@property (strong, nonatomic) UIView *fighter;

@property (nonatomic) float velocity;

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    [self createWalls];

    [self createFighter];

    

    [self start];

}

– (void)createWalls

{

    self.walls = [[NSMutableArray alloc] init];

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

        float x = (i%2) * 160 + 30;

        float y = (i/2) * 60 + 50;

        [self createWall:CGPointMake(x, y)];

    }

}

– (void)createWall:(CGPoint)p

{

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

        float x = (i % 10) * 10 + p.x;

        float y = (i / 10) * 10 + p.y;

        UIView *b = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 8)];

        b.backgroundColor = [UIColor brownColor];

        b.center = CGPointMake(x, y);

        [self.view addSubview:b];

        

        [self.walls addObject:b];

    }

}

– (void)createFighter

{

    self.fighter = [[UIView alloc] initWithFrame:CGRectMake(120, 400, 80, 60)];

    UIView *head = [[UIView alloc] initWithFrame:CGRectMake(25, 0, 30, 20)];

    head.backgroundColor = [UIColor whiteColor];

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

    body.backgroundColor = [UIColor whiteColor];

    [self.fighter addSubview:head];

    [self.fighter addSubview:body];

    [self.view addSubview:self.fighter];

    

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

    [self.fighter addGestureRecognizer:tap];

    

    self.velocity = 1;

}

– (void)shot:(UITapGestureRecognizer*)gr

{

    UIView *ball = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];

    ball.backgroundColor = [UIColor whiteColor];

    ball.layer.cornerRadius = 5;

    ball.center = CGPointMake(self.fighter.center.x, self.fighter.center.y40);

    [self.view addSubview:ball];

    

    [self.balls addObject:ball];

    

}

– (void)start

{

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

}

– (void)tick:(NSTimer*)sender

{

    CGPoint current = self.fighter.center;

    

    if (current.x > 320) {

        self.velocity = –1;

    }

    

    if (current.x < 0) {

        self.velocity = 1;

    }

    

    self.fighter.center = CGPointMake(self.fighter.center.x + self.velocity, self.fighter.center.y);

    

    for (UIView *b in self.balls) {

        b.center = CGPointMake(b.center.x, b.center.y2);

        

        for (UIView *w in self.walls) {

            if (CGRectIntersectsRect(b.frame, w.frame)) {

                [UIView transitionWithView:w duration:0.1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{

                    b.alpha = 0;

                } completion:^(BOOL finished) {

                    [b removeFromSuperview];

                    [w removeFromSuperview];

                    [self.balls removeObject:b];

                    [self.walls removeObject:w];

                }];

            }

        }

    }

}

– (NSMutableArray *)balls

{

    if (!_balls) {

        _balls = [[NSMutableArray alloc] init];

    }

    return _balls;

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end