I hope this is not a dumb question, since I’ve already googled and read a lot of other similar questions, but I have been messing around with this issue for days.
I have implemented a countdown timer using NSTimer and UIProgressView, but I want to do more with this time, i.e. play an alert sound when the timer reaches the last 10 seconds.
Here’s what I’ve done:
-(void) updateTimer
{
if (time <= 0.0f)
{
//Invalidate timer when time reaches 0
[timer invalidate];
[[AudioManager sharedManager] muteGameClock1Sound];
[self showGameOverViewController];
}
else
{
time -= 0.1;
[timeProgressView setProgress:time/totalTime animated:YES];
if (time < 10 && pauseView)
{
[[AudioManager sharedManager] playGameClock1Sound];
}
}
}
and in the viewDidLoad:
timeProgressView.progress = 1;
time = [ConfigFileManager sharedManager].timePerQuestion.intValue * [DatabaseManager sharedManager].numberOfQuestions;
totalTime = [ConfigFileManager sharedManager].timePerQuestion.intValue * [DatabaseManager sharedManager].numberOfQuestions;
timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f target: self selector: @selector(updateTimer) userInfo: nil repeats: YES];
and the pauseGame method:
-(IBAction)pauseGame:(id)sender
{
self.view.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.4];
pauseView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 250, 150)];
pauseView.backgroundColor = [UIColor colorWithWhite: 0.0 alpha: 0.4];
pauseView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
pauseView.clipsToBounds = YES;
if ([pauseView.layer respondsToSelector: @selector(setCornerRadius:)])
[(id) pauseView.layer setCornerRadius: 10];
resumeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
resumeButton.center = CGPointMake(pauseView.bounds.size.width/4, pauseView.bounds.size.height/2);
[resumeButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackground] forState:UIControlStateNormal];
[resumeButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackgroundSelected] forState:UIControlStateSelected];
[resumeButton setImage:[UIImage imageNamed:[ConfigFileManager sharedManager].messageResumeButton] forState:UIControlStateNormal];
[resumeButton addTarget:self action:@selector(resumeTheGame) forControlEvents:UIControlEventTouchUpInside];
restartButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
restartButton.center = CGPointMake(pauseView.bounds.size.width/2, pauseView.bounds.size.height/2);
[restartButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackground] forState:UIControlStateNormal];
[restartButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackgroundSelected] forState:UIControlStateSelected];
[restartButton setImage:[UIImage imageNamed:[ConfigFileManager sharedManager].messageReplayButton] forState:UIControlStateNormal];
[restartButton addTarget:self action:@selector(restartTheGame) forControlEvents:UIControlEventTouchUpInside];
quitButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
quitButton.center = CGPointMake(pauseView.bounds.size.width*3/4, pauseView.bounds.size.height/2);
[quitButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackground] forState:UIControlStateNormal];
[quitButton setBackgroundImage:[UIImage imageNamed:[ConfigFileManager sharedManager].buttonBackgroundSelected] forState:UIControlStateSelected];
[quitButton setImage:[UIImage imageNamed:[ConfigFileManager sharedManager].messageQuitButton] forState:UIControlStateNormal];
[quitButton addTarget:self action:@selector(showGameOverViewController) forControlEvents:UIControlEventTouchUpInside];
[pauseView addSubview:resumeButton];
[pauseView addSubview:restartButton];
[pauseView addSubview:quitButton];
[self.view addSubview: pauseView];
choice1Button.userInteractionEnabled = NO;
choice2Button.userInteractionEnabled = NO;
choice3Button.userInteractionEnabled = NO;
choice4Button.userInteractionEnabled = NO;
choice5Button.userInteractionEnabled = NO;
pauseButton.userInteractionEnabled = NO;
}
The problem is that the playGameClock1Sound method is not being called in this way, but it is, when I say: if (!pauseView && time > 10) which is not rational, and then in this case, when the pauseButton is pressed and therefore the pauseView is TRUE, the playGameClock1Sound method is being called again.
I’d be really grateful if someone helps me with this stupid issue, and even more grateful if you tell me how to handle pause/resume, too.
Can you show how pauseView is set? If you try something like this it may be simpler…
Everything you are doing seems fine so i think the issue is with how you are setting the pauseView bool.
Also, is the audio file you are playing shorter then or equal to the timers delay time? If it is not it will be played again before it is finished.