I am creating a simple tennis game in Objective-C. Currently, when the player or the computer scores, it gives a message “tap to begin” and that releases the ball (it’s a bit awkward to have your thumb on the racquet and another thumb to tap it to begin).
How do I add a timer into my game so that I don’t have to tap after every score?
(This code is the resetting method when there is a new game and when there is a score)
{
self.gameState = kGameStatePaused;
ball.center = self.view.center;
if(newGame)
{
if(computerScoreValue > playerScoreValue)
{
tapToBegin.textAlignment = UITextAlignmentCenter;
tapToBegin.text = @"Computer Wins!";
AudioServicesPlaySystemSound(booFileID);
}
else
{
tapToBegin.textAlignment = UITextAlignmentCenter;
tapToBegin.text = @"You Win!";
AudioServicesPlaySystemSound(clappingFileID);
}
computerScoreValue = 0;
playerScoreValue = 0;
}
else
{
tapToBegin.textAlignment = UITextAlignmentCenter;
tapToBegin.text = @"Tap to Begin";
}
playerScore.text = [NSString stringWithFormat:@"%d",playerScoreValue];
computerScore.text = [NSString stringWithFormat:@"%d",computerScoreValue];
}
NSTimer should work. If you’re curious take a look at CADisplayLink too.