Below is some of the code for a free app I'm creating for my pupils at school. It is very simple and counts the number of times they tap the screen within ten seconds. I have one timer countDownTimer
which counts down from 3 to 0 and then sets off my next timer 'myTimer' which then does the main countdown from 10 - 0. Everything works great in my app except the touches began starts when the first timer has been set off and I only want it to work when the second has been set off (for the 10 seconds).
Can anyone see where I've gone wrong?
#import "newgameViewController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "ViewController.h"
@implementation newgameViewController
@synthesize tapStatus, score, time, countDown;
-(IBAction)start {
[myTimer invalidate];
score.text= @"";
time.text= @"10";
tapStatus.text= @"";
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"3";
countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:@selector(showActivityCountDown) userInfo:nil repeats:YES];
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
-(IBAction)stop{
[myTimer invalidate];
myTimer = nil;
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"3";
}
-(IBAction)reset {
[myTimer invalidate];
myTimer = nil;
score.text= @"";
time.text= @"10";
tapStatus.text= @"";
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"3";
}
-(void)showActivityCountDown {
int currentTimeCount = [countDown.text intValue];
int newTimeCount = currentTimeCount - 1;
countDown.text = [NSString stringWithFormat:@"%d", newTimeCount];
if(currentTimeCount == 3)
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
else if(currentTimeCount == 2)
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
}
else if(currentTimeCount == 1)
{
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle,
(CFStringRef) @"beep", CFSTR ("wav"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
[countDownTimer invalidate];
countDownTimer = nil;
countDown.text= @"Go!";
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(showActivity) userInfo:nil repeats:YES];
}
}
-(void)showActivity {
float currentTime = [time.text floatValue];
float newTime = currentTime - 0.1;
time.text = [NSString stringWithFormat:@"%.1f", newTime];
if(currentTime == 0.0)
{
[myTimer invalidate];
myTimer = nil;
time.text= @"STOP!";
score.text = tapStatus.text;
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (myTimer != nil) {
NSUInteger tapCount = [[touches anyObject] tapCount];
tapStatus.text = [NSString stringWithFormat:@"%d taps", tapCount];
}
}
It seems like using
tapCount
is not going to be accurate for what you want--it returnsand it's meant to detect a gesture like a double- or triple-tap. It starts at the point the gesture recognition system decides it's a new gesture.
Instead of that, why don't you keep your own counter in a property:
Then in your
touchesBegan:withEvent:
, increment that counter: