はしりまわる、黄色い四角を
グリーンレーザーでうちまくれ!
という感じの子供向けiPhoneゲームのサンプルコードを書いてみた。

ポイント
ぽわーっとネオンというか、光った感じのUIViewは、
背景を黒にして、layer.shadowXXXXを設定することでそういう雰囲気にしてます。
格子の交差点に20×20のTapGestureを仕込んだUIViewを配置
たたくと、そこに向けて四方からレーザが出るようにしました。

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

iPhoneゲームシューティングサンプル

サンプルコード


#import “ViewController.h”

#import <QuartzCore/QuartzCore.h>

@interface ViewController () {

    UIView *mob;

    NSMutableArray *greenBar;

    BOOL hit;

}

@end

@implementation ViewController

– (void)viewDidLoad

{

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor blackColor];

    

    [self createLines];

    [self createButtons];

    

    [self createMob];

}

– (void)createLines

{

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

        float x = (i % 5) * 50 + 50;

        float y = (i / 5) * 50 + 50;

        

        UIView *l = [self neonLine];

        l.center = CGPointMake(x, y);

        

        UIView *l90 = [self neonLine];

        l90.center = CGPointMake(x, y);

        l90.transform = CGAffineTransformMakeRotation(M_PI/2.0);

    }

}

– (UIView*)neonLine

{

    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 3)];

    line.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1];

    line.layer.shadowColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1].CGColor;

    line.layer.shadowOffset = CGSizeMake(0, 0);

    line.layer.shadowRadius = 5.0;

    line.layer.shadowOpacity = 1.0;

    [self.view addSubview:line];

    return line;

}

– (void)createButtons

{

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

        float x = (i % 5) * 50 + 50;

        float y = (i / 5) * 50 + 50;

        UIView *btn = [self neonButton];

        btn.center = CGPointMake(x, y);

    }

}

– (UIView*)neonButton

{

    UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];

    btn.layer.cornerRadius = 5;

    btn.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1];

    btn.layer.shadowColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1].CGColor;

    btn.layer.shadowOffset = CGSizeMake(0, 0);

    btn.layer.shadowRadius = 5.0;

    btn.layer.shadowOpacity = 1.0;

    btn.alpha = 0.8;

    [self.view addSubview:btn];

    

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

    [btn addGestureRecognizer:tap];

    

    return btn;

}

– (void)tap:(UITapGestureRecognizer*)gr

{

    CGPoint p = gr.view.center;

    

    CGPoint start[] = {CGPointMake(300, 0), CGPointMake(-300, 0), CGPointMake(0, 300), CGPointMake(0, –300)};

    

    greenBar = [[NSMutableArray alloc] init];

    

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

        UIView *a = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 5)];

        a.backgroundColor = [UIColor greenColor];

        a.layer.shadowColor = [UIColor greenColor].CGColor;

        a.layer.shadowOffset = CGSizeMake(0, 0);

        a.layer.shadowRadius = 8.0;

        a.layer.shadowOpacity = 1.0;

        a.alpha = 0.4;

        a.center = p;

        

        float x = start[i].x;

        float y = start[i].y;

        if (i>1) {

            a.transform = CGAffineTransformRotate(a.transform, M_PI/2.0);

        }

        a.center = CGPointMake(a.center.x + x, a.center.y + y);

        [self.view addSubview:a];

        

        [greenBar addObject:a];

        

        [UIView animateWithDuration:0.5 animations:^{

            a.center = CGPointMake(a.center.x – x, a.center.y – y);

        } completion:^(BOOL finished) {

            [UIView animateWithDuration:0.2 animations:^{

                a.transform = CGAffineTransformMakeScale(0, 0);

            } completion:^(BOOL finished) {

                [a removeFromSuperview];

                a.center = CGPointMake(-100, –100);

            }];

        }];

    }

}

– (void)createMob

{

    mob = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];

    mob.backgroundColor = [UIColor yellowColor];

    mob.layer.cornerRadius = 10;

    mob.userInteractionEnabled = NO;

    

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

    eyeR.backgroundColor = [UIColor blueColor];

    eyeR.layer.cornerRadius = 5;

    [mob addSubview:eyeR];

    

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

    eyeL.backgroundColor = [UIColor blueColor];

    eyeL.layer.cornerRadius = 5;

    [mob addSubview:eyeL];

    

    

    UIView *mouth = [[UIView alloc] initWithFrame:CGRectMake(10, 40, 30, 3)];

    mouth.backgroundColor = [UIColor redColor];

    [mob addSubview:mouth];

    

    [self.view addSubview:mob];

    

    

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

    

    

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

}

– (void)move:(NSTimer*)sender

{

    int dx = arc4random() % 10049;

    int dy = arc4random() % 10049;

    if (CGRectContainsPoint(CGRectMake(40, 40, 220, 360), CGPointMake(mob.center.x + dx, mob.center.y + dy))) {

        [UIView animateWithDuration:30.0/60.0 animations:^{

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

        }];

    }

}

– (void)hitcheck:(NSTimer*)sender

{

    for (UIView *gLine in greenBar) {

        if (!hit && CGRectIntersectsRect(mob.frame, [gLine.layer.presentationLayer frame])) {

            

            hit = YES;

            

            [UIView animateWithDuration:0.5 animations:^{

                mob.transform = CGAffineTransformRotate(mob.transform, M_PI/2.0);

            } completion:^(BOOL finished) {

                hit = NO;

            }];

        }

    }

}

– (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end