After the end of our game the NSTimer does not stop.
-(void)StartGame{
Countdown = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimeDecrease) userInfo:nil repeats:YES];
}
-(void)TimeDecrease{
StartTime = StartTime - 1;
Timer.text = [NSString stringWithFormat:#"%d",StartTime];
if(StartTime == 0){
[self EndGame];
}
}
We tried [Countdown release] and disabled ARC but it throws an error.
We are trying to accomplish a timer that resets to 15 at the beginning of every new game.
Try:
[Countdown invalidate];
Countdown = nil;
Related
When I hold down on a button my image will move to a direction.
The game objective is that they are going to move image to hit another image, and to do that I'm using an NSTimer.
Then they also have another NSTimer because they only have 15 seconds on them to hit the other image. If they don't the game will [self gameover] but, the NSTimer that counts the 15 seconds is started and I move the image the image moves a bit and then it goes back to center again.
When I delete the countdown timer, the image does not go back to center after I stop pressing the button but it stops on its current position.
And that's what I want it to do while I'm using the countdown timer as well.
Code:
-(void)goLeft
{
ball.center = CGPointMake(ball.center.x -5, ball.center.y);
}
-(IBAction)left
{
goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
if (goLeft == nil) goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:#selector(goLeft) userInfo:nil repeats:YES];
}
-(IBAction)stopLeft
{
[goLeft invalidate];
goLeft = nil;
}
//And heres the timer :
-(IBAction)doCountdown: (id)sender;{
if (countdownTimer)
return;
remainingTicks = 25;
[self updateLabel];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: #selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void)updateLabel
{
theLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}
I have implemented some code that allows a user to set a time limit to countdown from using UIDatePicker, the users then presses a "Start" button and the countdown is printed in to a UILabel.
I am trying to find a way to stop the timer. Here is the code I have so far that starts the timer:
#implementation P11DetailController
int afterRemainder;
int iRemainder;
NSTimeInterval countDownInterval;
- (void)updateCountDown{
afterRemainder --;
int hours = (int)(afterRemainder)/(60*60);
int mins = (int)(((int)afterRemainder/60) - (hours * 60));
int secs = (int)(((int)afterRemainder - (60 * mins) - ( 60*hours*60)));
NSString *displayText = [[NSString alloc] initWithFormat:#"%02u : %02u :
%02u", hours, mins, secs];
self.displayLabel.text = displayText;
}
then when the user user presses "start":
- (IBAction)startButton:(id)sender {
countDownInterval = (NSTimeInterval)_countdownTimer.countDownDuration;
iRemainder = countDownInterval;
afterRemainder = countDownInterval - iRemainder%60;
[NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:#selector(updateCountDown) userInfo:nil repeats:YES];
}
and finally, when the users presses "Stop":
- (IBAction)stopButton:(id)sender {
//not sure what to add here
}
any ideas?
You need to keep a reference to NSTimer as an ivar:
#implementation P11DetailController
{
NSTimer *myTimer;
}
then:
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
selector:#selector(updateCountDown) userInfo:nil repeats:YES];
Then a simple call to:
[myTimer invalidate];
Will stop the timer.
This is all in the documentation which you should consult first.
I have the right code to move an object randomly and also to make numbers count down but I am not able to have an object move while the timer counts down. One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down.
The main question is: how can I have a timer countdown and have the object move at the same time?
(my main goal is to make the object stop moving once the timer reaches zero)
I would appreciate it alot if someone could help with this problem?
-(void)Workauto{
secondsCount1 = 10;
autoperiods = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(autoperiod) userInfo:nil repeats:YES];
}
-(void)autoperiod{
secondsCount1 = secondsCount1 -1;
int minuts = secondsCount1/ 60;
int seconds = secondsCount1 - (minuts * 60);
NSString *timerOutput = [NSString stringWithFormat:#"%2d:%.2d", minuts , seconds];
Count.text = timerOutput;
if (secondsCount1 == 0){
secondsCount1=0;
[autoperiods invalidate];
autoperiods=nil;
Count.hidden=YES;
AutoTimeLeftLabel.hidden=YES;
TimeLeft.hidden=NO;
TimeLeftlabel.hidden=NO;
Ball.hidden=NO;
Ball2.hidden=NO;
BluBall.hidden=NO;
BluBall2.hidden=NO;
RedHigh1.hidden=YES;
RedHigh2.hidden=YES;
RedHigh3.hidden=YES;
RedHigh4.hidden=YES;
RedLow1.hidden=YES;
RedLow2.hidden=YES;
BlueHigh1.hidden=YES;
BlueHigh2.hidden=YES;
BlueHigh3.hidden=YES;
BlueHigh4.hidden=YES;
BlueLow1.hidden=YES;
BlueLow2.hidden=YES;
RedMid1.hidden=YES;
RedMid2.hidden=YES;
RedMid3.hidden=YES;
RedMid4.hidden=YES;
BlueMid1.hidden=YES;
BlueMid2.hidden=YES;
BlueMid3.hidden=YES;
BlueMid4.hidden=YES;
move = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:#selector(move) userInfo:nil repeats:YES];
pos = CGPointMake(4.0, -4.0);
move2 = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:#selector(move2) userInfo:nil repeats:YES];
pos2 = CGPointMake(3.0, 4.0);
Update = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
DPad = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(DPad) userInfo:nil repeats:YES];
//RedGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(RedGoals) userInfo:nil repeats:YES];
//BlueGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:#selector(BlueGoals) userInfo:nil repeats:YES];
[self SetTimer];
}
}
-(void)SetTimer{
comeonandcount = 150;
GameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(GameTimerCo) userInfo:nil repeats:YES];
}
-(void)GameTimerCo{
comeonandcount = comeonandcount - 1;
int minuts = comeonandcount / 60;
int seconds = comeonandcount - (minuts * 60);
NSString *timerOutputGame = [NSString stringWithFormat:#"%2d:%.2d", minuts , seconds];
TimeLeft.text = timerOutputGame;
if (comeonandcount == 0){
comeonandcount=0;
[GameTimer invalidate];
GameTimer=nil;
[move invalidate];
[move2 invalidate];
}
}
One thing I found weird though was that when I took out the part about the TimeLeft label the objects moved randomly but obviously the numbers did not count down.
Very well done! You've actually solved the problem already. That part is not "weird"; it's the cause of the problem.
You are using autolayout. Well, when you set the text of a label, that causes layout to happen. Thus, the constraints throughout your interface assert themselves and put all your objects back where they were. It's as simple as that.
This question already has answers here:
How can I programmatically pause an NSTimer?
(16 answers)
Closed 9 years ago.
I have a game which uses a timer. I want to make it so the user can select a button and it pauses that timer and when they click that button again, it will unpause that timer. I already have code to the timer, just need some help with the pausing the timer and the dual-action button.
Code to timer:
-(void)timerDelay {
mainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(countDownDuration)
userInfo:nil
repeats:YES];
}
-(void)countDownDuration {
MainInt -= 1;
seconds.text = [NSString stringWithFormat:#"%i", MainInt];
if (MainInt <= 0) {
[timer invalidate];
[self delay];
}
}
That's very easy.
// Declare the following variables
BOOL ispaused;
NSTimer *timer;
int MainInt;
-(void)countUp {
if (ispaused == NO) {
MainInt +=1;
secondField.stringValue = [NSString stringWithFormat:#"%i",MainInt];
}
}
- (IBAction)start1Clicked:(id)sender {
MainInt=0;
timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(countUp) userInfo:Nil repeats:YES];
}
- (IBAction)pause1Clicked:(id)sender {
ispaused = YES;
}
- (IBAction)resume1Clicked:(id)sender {
ispaused = NO;
}
There is no pause and resume functionality in NSTimer. You can impliment it like below code.
- (void)startTimer
{
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)fireTimer:(NSTimer *)inTimer
{
// Timer is fired.
}
- (void)resumeTimer
{
if(m_pTimerObject)
{
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
m_pTimerObject = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(fireTimer:) userInfo:nil repeats:YES];
}
- (void)pauseTimer
{
[m_pTimerObject invalidate];
m_pTimerObject = nil;
}
I have this app which has a timer in it and I want to be able to display a button at random points in that timer. Sort Of like a game setting where you have to get the button to get more time before it disappears. I already have some code , but the problem is that the timer that i already have is being messed up ( i can tell because it is linked to a label. It messed up because it goes down by 2 instead of 1 and this only happens when i have the code to make the button appear and disappear with the timer.
Here is the code to my timer that is being messed up:
-(IBAction)Ready:(id)sender {
[self performSelector:#selector(TimerDelay) withObject:nil afterDelay:1.0];
[self performSelector:#selector(randomTimer) withObject:nil afterDelay:0.5];
}
-(void)TimerDelay {
MainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(countDownDuration)
userInfo:nil
repeats:YES];
}
Here is the code to the button:
-(IBAction)Ready:(id)sender { //the same ready action as above
[self performSelector:#selector(TimerDelay) withObject:nil afterDelay:1.0];
[self performSelector:#selector(randomTimer) withObject:nil afterDelay:0.5];
}
-(void)TimerDelay {
MainInt = 36;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(countDownDuration)
userInfo:nil
repeats:YES];
}
- (void)randomTimer {
NSInteger randomTime = arc4random_uniform(0); //Some number between 0 and 9
timer = [NSTimer scheduledTimerWithTimeInterval:randomTime
target:self
selector:#selector(showButton)
userInfo:nil
repeats:NO];
}
- (void)showButton {
if(self.plus5.hidden == YES) {
self.plus5.hidden = NO;
}
else {
self.plus5.hidden = YES;
}
[self TimerDelay]; //Call random timer function again to run this method at a future random time
}
You are calling "randomTimer" and "TimerDelay" (this should be "timerDelay") one after the other and scheduling and REscheduling "timer".
I think you should reconsider your architecture, here...
EDIT: Use just one timer and let it manage your button.
- (void)somewhereInYourCode
{
shouldShowButton = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timeFired)
userInfo:nil
repeats:YES];
}
- (void)timeFired
{
// Here your countdown and...
if (shouldShowButton) {
NSInteger randomTimeInt = arc4random_uniform(9); // as suggested by Bergasms
float randomTimeFloat = randomTimeInt;
[self performSelector:#selector(showButton) withObject:nil afterDelay:randomTimeFloat];
shouldShowButton = NO;
}
}
This way your timer won't (probably) mess up.
This:
arc4random_uniform() will return a uniformly distributed random number
less than upper_bound.
is one of your problems.
NSInteger randomTime = arc4random_uniform(0); //Some number between 0 and 9
needs to be
NSInteger randomTime = arc4random_uniform(9); //Some number between 0 and 9
I cannot say for the rest of your code. But the line you have will always return 0, which means the random timer will always execute instantly.