Thank you in advance.
I am developing a game that runs on a timer. I have a label on the view *countdown. I am using a NSTimer *countdownTimer to run 120 seconds (2:00). That is when the game is over. Also, at the same time...I am running a transition timer that modals to another view controller.
I have it set up where the next view the player can select "Play Again". I have it set up where it goes back to the original view. However, how do I reset the *countdownTimer to run again from 2:00?
- (IBAction)playAgain:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
[self setTimer];
[self GameOver];
[self Score];
[self timerRun];
countdown.text = [NSString stringWithFormat:#"2:00"];
}
This is the button to play again. The countdown text does not reset. Here is my NSTimer for *countdownTimer
-(void) timerRun {
secondsCount = secondsCount - 1;
int minuts = secondsCount / 60;
int seconds = secondsCount - (minuts * 60);
NSString *timerOutput = [NSString stringWithFormat: #"%2d:%.2d", minuts, seconds];
countdown.text = timerOutput;
if (secondsCount == 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void) setTimer {
secondsCount= 120;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerRun) userInfo:nil repeats:YES];
When it resets, it just stays at zero instead of going back to 2:00
Thank you for your help.
Related
I have a requirment of 15sec timer in my app in the circular progress bar sort of animation which will contain label in the center of seconds remain
Following code gives the labelprogress from 0.0 to 1.0f but I want to map it to 15 seconds countdown with the smooth animation also if there is timer extender than it should get the time added with the smooth animation
ex: Firstly I need to start from 15 seconds and if user clicks on the timeextender powerup and current time is the 10sec then it should be 25 sec and acoordingly the progressbar percentage should work.
So what logic I need to map to get the above requirement done
- (void)progressChange
{
// Labeled progress views
NSArray *labeledProgressViews = #[//self.labeledProgressView,
self.labeledLargeProgressView];
for (DALabeledCircularProgressView *labeledProgressView in labeledProgressViews) {
CGFloat progress = ![self.timer isValid] ? self.stepper.value / 10.0f : labeledProgressView.progress + 0.01f;
[labeledProgressView setProgress:progress animated:YES];
labeledProgressView.progressLabel.text = [NSString stringWithFormat:#"%.2f", labeledProgressView.progress];
}
}
- (void)startAnimation
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.03
target:self
selector:#selector(progressChange)
userInfo:nil
repeats:YES];
self.continuousSwitch.on = YES;
}
- (void)stopAnimation
{
[self.timer invalidate];
self.timer = nil;
self.continuousSwitch.on = NO;
}
- (IBAction)TimeExtender:(id)sender
{
if (labeledProgressView.progress >= 1.0f && [self.timer isValid]) {
[labeledProgressView setProgress:0.f animated:YES];
}
}
I solved it by myself here's the code
- (void)progressChange
{
CGFloat progress ;
DALabeledCircularProgressView *labeledProgressView = self.labeledLargeProgressView;
if(labeledProgressView.progress >=1.0f && [self.timer isValid]){
[self stopAnimation];
seconds = 15.0f;
}
else{
progress=labeledProgressView.progress + 0.06666667f;
[labeledProgressView setProgress:progress animated:YES];
seconds --;
labeledProgressView.progressLabel.text = [NSString stringWithFormat:#"%i", seconds];
}
}
- (void)startAnimation
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(progressChange)
userInfo:nil
repeats:YES];
self.continuousSwitch.on = YES;
}
So I am trying to make a countdown and when its done the game is over. Here is my code:
-(void)Movement667{
loadinglabel.text = [NSString stringWithFormat:#"Go!"];;
end =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(Movement999)userInfo:nil repeats:YES];
}
-(void)Movement999{
if (gametime > 0){
gametime = gametime - 1;
gametimelabel.text = [NSString stringWithFormat:#"%i", gametime];
}else{
smallrock6.hidden = YES;
endlabel.hidden = NO;
highscore.hidden = NO;
highscoreannouncer.hidden = NO;
highscorebackground.hidden = NO;
highscore.text = [NSString stringWithFormat:#"%i", score];
}
}
The first time I play it everything works fine, and it counts down every second. When I play it a second time and so on it starts off normal then it counts down in greater patterns i.e. 2, 4 , 5 ect. Does anyone know why?
It's because you never stop the timer. So what happens is that when you reset the time and create a new timer there are two timers running at the same time (the selector is called twice per second), then three, four, etc. Try to keep the timer in a property and change the code as follows:
#property (strong, nonatomic) NSTimer *myTimer;//ADDDED
-(void)Movement667{
loadinglabel.text = [NSString stringWithFormat:#"Go!"];;
self.myTimer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(Movement999)userInfo:nil repeats:YES];//CHANGED
}
-(void)Movement999{
if (gametime > 0){
gametime = gametime - 1;
gametimelabel.text = [NSString stringWithFormat:#"%i", gametime];
}else{
[self.myTimer invalidate];//ADDED
smallrock6.hidden = YES;
endlabel.hidden = NO;
highscore.hidden = NO;
highscoreannouncer.hidden = NO;
highscorebackground.hidden = NO;
highscore.text = [NSString stringWithFormat:#"%i", score];
}
}
ADDED: If you need to interrupt your game before you run out of time you should make sure to call [self.myTimer invalidate] in that case as well.
ADDED: If you want to make sure the timer is also invalidated when your ViewController goes off screen I would suggest invalidate the timer in viewDidDisappear:
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[self.myTimer invalidate];
}
When the game is done, don't forget to invalidate your time with
[end invalidate];
Put this code in the else statement in Movement999.
It stops the receiver from ever firing again. And then you can restart the timer for a new game.
In my viewController I inserted a timer for a calculation of numbers taken directly from my database of Parse.com.
The timer is working correctly, were inserted in viewDidAppear and in ViewDidDisappear in such a way as to lock the sequence when changing view controller.
The problem I have is that when I change the view controller with the Push the timer does not stop you give an example:
I open the application and the calculation is done correctly with the animation of NSTimer.
Change View, and then I go back with the back button to view where the timer at this point I see that the numbers contiunano to increase every time I change view and go back ...
Can you explain where I'm wrong?
-(void)viewDidAppear:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
self.title = NSLocalizedString(#"", nil);
[self animatelayertopoint:0];
[self AnimationMenuView:600];
[self QueryForViewPrincipal];
[self QueryForCollectionView];
[self ShowBadgeNumber];
[self Timer];
[collectionView reloadData];
}
-(void)Timer{
timer = [NSTimer scheduledTimerWithTimeInterval:0.012 target:self selector:#selector(CalcoloMediadiLaurea) userInfo:nil repeats:YES];
}
-(void) CalcoloMediadiLaurea{
PFUser *CurrentUser = [PFUser currentUser];
NSNumber *NumeroUndici = [NSNumber numberWithInt:11];
NSNumber *NumeroTre = [NSNumber numberWithInt:3];
NSNumber *ValoreMediaPonderata = [CurrentUser objectForKey:FF_USER_MEDIA_PONDERATA];
int FFValoreTre = [NumeroTre intValue];
int FFValoreUndici = [NumeroUndici intValue];
double MediaPonderata = [ValoreMediaPonderata doubleValue];
double RisultatoMoltiplicazioneValoriPonderataUndici;
RisultatoMoltiplicazioneValoriPonderataUndici = MediaPonderata * FFValoreUndici;
double RisultatoMediaLaurea;
RisultatoMediaLaurea = RisultatoMoltiplicazioneValoriPonderataUndici / FFValoreTre;
CalcoloMediaLaureaLabel.text = [NSString stringWithFormat:#"%.1f", FFTickValoreMediaLaurea ];
FFTickValoreMediaLaurea++;
if(FFTickValoreMediaLaurea > RisultatoMediaLaurea){
[timer invalidate];
timer = nil;
}
}
-(void)viewDidDisappear:(BOOL)animated {
[self animatelayertopoint:0];
[self AnimationMenuView:600];
[self CalcoloMediadiLaurea];
}
The timer get rescheduled again on view did appear without getting invalidated to the previous one.You should make instance of timer in .h and then hold the reference of timer in it .And on view did appear when you are scheduling the timer just invalidate it and then schedule it again.
if([self.timer isValid])
{
[self.timer invalidate];
self.timer=nil;
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(CalcoloMediadiLaurea:) userInfo:nil repeats:YES];
}
This worked for me which is along the lines of the above answer but a little more explicitly stated.
- (void)viewWillAppear:(BOOL)animated
{
NSTimeInterval secondsBetween = [endDate timeIntervalSinceDate:currentTime];
secondsLeft = secondsBetween;
[self countdownTimer];
}
- (void)viewDidDisappear:(BOOL)animated
{
[self.timer invalidate];
self.timer = nil;
}
//Initiates timer
- (void)countdownTimer
{
[self.timer invalidate];
self.timer = nil;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(updateCounter:) userInfo:nil repeats:YES];
}
It's because you are creating new timers without invalidating old ones. The timers don't get invalidated automatically, you have to do it yourself. So if U go on second view and return before your invalidate condition is true you will create new timer and have two of them :)... And so on.
I have a simple timer using that is activated with a button is pushed. It runs from 60 to 0 no problem but what I want is to stop and reset the timer on push button. I have managed to stop it when the button is pressed using the code below but for some reason cannot get it to reset and stop at 60. This should be simple but it isn't working. Any suggestions?
Timer is set using simple Action
- (IBAction)timerStart:(id)sender {
if(!secondCounter == 0){
[countdownTimer invalidate];
}
else {
[self setTimer];
}
}
CODE FOR TIMER
- (void)timerRun {
secondCounter = secondCounter - 1;
int minutes = secondCounter;
NSString *timerOutput = [NSString stringWithFormat:#"%d", minutes ];
countDownLabel.text = timerOutput;
if(secondCounter == 0){
[countdownTimer invalidate];
countdownTimer = nil;
}
}
- (void)setTimer {
secondCounter = 60;
countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(timerRun) userInfo:nil repeats:YES];
}
You need to set the seconds down to 0, otherwise the you always invalidate your timer, but never start it again:
- (IBAction)timerStart:(id)sender {
if(!secondCounter == 0) {
[countdownTimer invalidate];
secondCounter = 0;
}
else {
[self setTimer];
}
}
What do you mean to stop the timer? Timer is not going to stop. You would like to have to have countDownLabel.text to show 60? When you invalidate the timer it will stop running and calling timerRun method If you want to restart the timer just call setTimer function again. I think what you meant is to have countDownLabel.text = #"60" after [countdownTimer invalidate]; and when you click the button again the timer will start again and will change the label. Let me know if this is what you were looking for.
As Christian said you have to reset the secondCounter
secondCounter = 0;
I made a stopwatch application recently and it had a few glitches.
If I hit the stop button twice in a row, the entire app would crash.
If I hit the start button twice in a row, the timer would run twice as fast and the stop button would stop working.
How do I fix this problem?
Here is the code in my .h file:
IBOutlet UILabel *time;
IBOutlet UILabel *time1;
IBOutlet UILabel *time2;
NSTimer *myTicker;
NSTimer *myTicker2;
NSTimer *myTicker3;
}
- (IBAction)start;
- (IBAction)stop;
- (IBAction)reset;
- (void)showActivity;
- (void)showActivity1;
- (void)showActivity2;
#end
and here is my code in the .m file:
- (IBAction)start {
myTicker = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showActivity) userInfo:nil repeats:YES];
myTicker2 = [NSTimer scheduledTimerWithTimeInterval:.1 target:self selector:#selector(showActivity1) userInfo:nil repeats:YES];
myTicker3 = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:#selector(showActivity2) userInfo:nil repeats:YES];
}
- (IBAction)stop {
[myTicker invalidate];
[myTicker2 invalidate];
[myTicker3 invalidate];
}
- (IBAction)reset {
time.text = #"00";
time1.text = #"00";
time2.text = #"00";
}
- (void)showActivity {
int currentTime = [time.text intValue];
int newTime = currentTime + 1;
if (newTime == 60) {
newTime = 0;
}
time.text = [NSString stringWithFormat:#"%d", newTime];
}
- (void)showActivity1 {
int currentTime1 = [time1.text intValue];
int newTime1 = currentTime1 + 1;
if (newTime1 == 10) {
newTime1 = 0;
}
time1.text = [NSString stringWithFormat:#"%d", newTime1];
}
- (void)showActivity2 {
int currentTime2 = [time2.text intValue];
int newTime2 = currentTime2 + 1;
time2.text = [NSString stringWithFormat:#"%d", newTime2];
}
Set the stop button's userInterActionEnabled property to NO and the start button's to YES when the -stop method is fired. Then switch and set the stop button's userInterActionEnabled to YES and the start button's to NO when -start is fired.
You should create a private BOOL variable "isRunning", which is checked when clicked on Stop or Start like:
- (IBAction)stop {
if(!isRunning) return;
[myTicker invalidate];
[myTicker2 invalidate];
[myTicker3 invalidate];
self.isRunning = NO;
}
etc. Also ignoring user interactions is general a good idea (like CodaFi suggested), but only fights the symptoms ;-) You really should do both checks.