タイピングゲームアプリ〜完成
http://d.hatena.ne.jp/moto_maka/20081225/1230151823
を見ながらタイピングゲームアプリを作った。
ポイント
1.タイマーの開始と終了の条件設定
2.文字列の比較方法
ポイント1:タイマーの開始と終了の条件設定
・入力開始(キーボードが出たタイミング)と同時にタイマースタート
・入力終了後、Goを押したタイミングで入力文字列チェック。
-> 一致すればタイマーストップ。
-> 一致しなければタイマーを動かしたまま。
ポイント2:文字列の比較方法
文字列==文字列ではダメで、isEqualToStringという関数を使う。
NSString *strExample = self.lbl_example.text; NSString *strInput = self.textfield_input.text; if ([strExample isEqualToString:strInput]) { // 例題と入力した文章が一致! timeflg = FALSE; self.lbl_result.hidden = NO; self.lbl_result.text = @"正解!"; } else { // 入力ミス // タイマーを止めない。 self.lbl_result.hidden = NO; self.lbl_result.text = @"ミス!"; }
ここを参考にした。
注意点
もとまかさんのページのコメント欄にあるが、シミュレータでは日本語キーボードがでないっぽい。
TextEditのAttributes inspectorから、表示するキーボードの設定が行えるが、どれを選んでもシミュレータ上で日本語入力ができることを確認できなかった。
実機でやる必要があるのはXcode4.4でも変わっていないのか? そもそもXcode4.4がまだ日本語化されてないせいか。
不明。
作ってみて
タイマーの使い方について、基本的にずっと動いていてフラグによってそれを表示するかどうかを判断するだけ、みたいな形に少し慣れた気がする。
ソース
.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *lbl_result; @property (weak, nonatomic) IBOutlet UITextField *textfield_input; @property (weak, nonatomic) IBOutlet UILabel *lbl_time; @property (weak, nonatomic) IBOutlet UILabel *lbl_example; - (IBAction)OnBtn_Clear:(id)sender; - (IBAction)Did_End_On_Exit:(id)sender; - (IBAction)Editing_Did_Begin:(id)sender; -(void)onTimer:(NSTimer*)timer; @end
.m
@interface ViewController () @end @implementation ViewController @synthesize lbl_result; @synthesize textfield_input; @synthesize lbl_time; @synthesize lbl_example; NSDate *start_date; BOOL timeflg=FALSE; NSTimer *timer; - (void)onTimer:(NSTimer*)timer { if(timeflg){ NSDate *now = [NSDate date]; self.lbl_time.text = [NSString stringWithFormat:@"%.2f", [now timeIntervalSinceDate:start_date]]; } } - (void)viewDidLoad { [super viewDidLoad]; self.lbl_time.text = @"0.00"; self.lbl_example.text = @"テキストボックスをタップするとはじまるよ!!"; self.lbl_result.hidden = YES; timer = [NSTimer scheduledTimerWithTimeInterval:(0.01) target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]; } - (void)viewDidUnload { [self setLbl_example:nil]; [self setLbl_time:nil]; [self setTextfield_input:nil]; [self setLbl_result:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (IBAction)OnBtn_Clear:(id)sender { timeflg = FALSE; self.lbl_time.text = @"0.00"; self.lbl_example.text = @"テキストボックスをタップするとはじまるよ!!"; self.textfield_input.text = nil; } - (IBAction)Did_End_On_Exit:(id)sender { NSString *strExample = self.lbl_example.text; NSString *strInput = self.textfield_input.text; if ([strExample isEqualToString:strInput]) { // 例題と入力した文章が一致! timeflg = FALSE; self.lbl_result.hidden = NO; self.lbl_result.text = @"正解!"; } else { // 入力ミス // タイマーを止めない。 self.lbl_result.hidden = NO; self.lbl_result.text = @"ミス!"; } } - (IBAction)Editing_Did_Begin:(id)sender { self.lbl_result.hidden = YES; if (!timeflg){ start_date = [NSDate date]; timeflg = TRUE; } self.lbl_example.text = @"aaa"; } @end