指でタップするリズムゲームの落書き
(XcodeのiOS6 Simulatorで試しています。)
ポイント
・テンポ100で何となくあわせる
・メトロノーム的な音源はガレージバンドで作成
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>
@interface ViewController() {
CADisplayLink *timer;
AVAudioPlayer *myPlayer;
float tempo;
int count;
}
@property (nonatomic, strong) NSMutableArray *foots;
@property (nonatomic, strong) NSArray *jumpPattern;
@end
@implementation ViewController
@synthesize foots, jumpPattern;
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
}
– (void)start
{
timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateDisp:)];
[timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
// 同時に音楽も開始
[self playMySound];
}
– (void)viewDidAppear:(BOOL)animated
{
// 指でタップする位置
[self createLine];
// タップ配列の初期化
self.foots = [[NSMutableArray alloc] init];
// タップのパターン
[self createJumpPatten];
// テンポ
tempo = 100.0;
// タイマースタート
[self start];
}
– (void)createJumpPatten
{
// タップ位置
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:@”010″];
[arr addObject:@”010″];
[arr addObject:@”100″];
[arr addObject:@”010″];
[arr addObject:@”010″];
[arr addObject:@”001″];
self.jumpPattern = [arr copy];
}
– (void)updateDisp:(CADisplayLink*)sender
{
static float timeDelta;
timeDelta += sender.duration;
if (timeDelta >= 60 / tempo) {
timeDelta = 0;
[self createFoot];
}
for (UIView *foot in foots) {
foot.center = CGPointMake(foot.center.x, foot.center.y + 1);
}
}
– (void)createLine
{
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 350, 320, 60)];
line.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.6];
[self.view addSubview:line];
// center
UIView *centerline = [[UIView alloc] initWithFrame:CGRectMake(0, 25, 320, 5)];
centerline.backgroundColor = [UIColor whiteColor];
[line addSubview:centerline];
}
– (void)createFoot
{
// pattern から
int index = count % 6;
NSString *pattern = [self.jumpPattern objectAtIndex:index];
count++;
for (int i=0; i<[pattern length]; i++) {
if ([pattern characterAtIndex:i] == ‘1’) {
float x = i * 80 + 40;
UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(x, 0, 30, 30)];
foot.layer.cornerRadius = 15.0;
foot.backgroundColor = [UIColor blackColor];
[self.view addSubview:foot];
// center
UIView *centerline = [[UIView alloc] initWithFrame:CGRectMake(0, 15, 30, 5)];
centerline.backgroundColor = [UIColor whiteColor];
[foot addSubview:centerline];
[self.view addSubview:foot];
[foots addObject:foot];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[foot addGestureRecognizer:tap];
}
}
}
– (void)tap:(UITapGestureRecognizer*)tgr
{
UIView *foot = tgr.view;
float dif = abs(foot.center.y – 375);
// タップしたときラインがそろっているか
if (dif < 12) {
foot.backgroundColor = [UIColor greenColor];
[UIView animateWithDuration:0.5 animations:^{
foot.center = CGPointMake(400, 0);
}];
} else {
foot.backgroundColor = [UIColor redColor];
}
}
– (void)playMySound
{
// ファイル読み込み
NSData *fileData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”sound” ofType:@”mp3″]];
// player 準備
NSError *playbackError = nil;
myPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&playbackError];
// 再生
if (myPlayer != nil) {
[myPlayer prepareToPlay];
[myPlayer play];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end