How to reset countdown timer in spritekit - ios

How to reset countdown timer when the score is divisible by 10?
Here is the score method
-(void)setScore:(int)score
{
_score = score;
scoreLabel.text = [NSString stringWithFormat:#"Score: %d", score];
}
the update method that counts down time
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if (startGamePlay) {
startTime = currentTime;
startGamePlay = NO;
}
int countDownInt = 10.0 - (int)(currentTime - startTime);
timeLabel.text = [NSString stringWithFormat:#"%i", countDownInt];
if (self.score % 10 == 0) {
//reset countDownInt here
}
}

Related

Adding Value in variable (If statement)

I'm trying to figure this out, I have a counting app and want it to increase by 45. Assume the user clicks + 5 times, they can only subtract it -5 for it to equal 0.
Here is my if statement but it doesn't work, it goes into the negatives.
Can anyone help? It's not coming to me. It's the (if count >= 0)
-(IBAction)upButton45:(id)sender {
xCount1 +=1;
countNumber45 +=45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight += 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
-(IBAction)downButton45:(id)sender {
xCount1 -= 1;
countNumber45 -= 45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
if (countNumber45 <= 0) {
countNumber45 = 0;
xCount1 = 0;
x45Label.text = #"";
}
if (xCount1 >= 0) {
totalWeight -= 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)upButton45:(id)sender
{
if(xCount + 1 <= 45) //Your max allowed tap
{
xCount1 +=1;
countNumber45 +=45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight += 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)downButton45:(id)sender
{
if(xCount - 1 >= 0)
{
xCount1 -= 1;
countNumber45 -= 45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight -= 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)upButton45:(id)sender
{
//Assuming that the lable always hold value and have "0" as start value, and number is at first position
NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:#" "];
int currentValue = [(NSString *)stringList[0] intValue];
currentValue = currentValue + 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", currentValue];
}
-(IBAction)downButton45:(id)sender
{
//Assuming that the lable always hold a int value and number is at first position
NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:#" "];
int currentValue = [(NSString *)stringList[0] intValue];
if (currentValue != 0) {
currentValue = currentValue - 45;
}
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", currentValue];
}

Cant get my NSTimer to end when the value <= 0

i cant seem to get my NSTimer to stop when it reaches 0 i've looked around for an answer and i tryed everything please help.
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
}
-(IBAction)start:(id)sender {
play.hidden = YES;
mainint = 60;
timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(count) userInfo:nil repeats:YES];
if (timer1 == 0) {
[timer1 invalidate];
timer1 = nil;
}
}
put your if condition in count() function
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
if (mainint == 0) {
[timer1 invalidate];
}
}
The issue is that your timer will never be nil (0) unless you set it yourself to nil. I think what you're trying to achieve is something like this :
-(void)count {
mainint -= 1;
seconds.text = [NSString stringWithFormat:#"%i", mainint];
if (mainint <= 0) {
[timer1 invalidate];
timer1 = nil;
}
}

How to call a label to display if the score is below e.g.:90 points?

Im making a word game, there are two possible labels "Perfect!" which shows only when the score is 100 and "Correct!" when the score is below 100,
But I'm having trouble calling the "Correct!" label when the score goes below 100!
- (IBAction)btncheck:(id)sender {
NSString *answer = [_textbox.text stringByReplacingOccurrencesOfString:#" " withString:#""];
if([answer isEqualToString:#""]){
}
else
if ([answer isEqualToString:#"q"]) {
_keyboard.hidden = YES;
_textXclear.hidden = YES;
//Perfect button
[_closeone setHidden:NO];
[_wrongone setHidden:YES];
score = MAX (score +100, 0);
[scoreLabel setText:[NSString stringWithFormat:#"score: %d", score]];
coins = coins +5;
if (score == 100) coins = 8;
if (score == 0) coins = 0;
if (score == 4) coins = 4;
if (score == 3) coins = 3;
if (score == 2) coins = 2;
if (score == 1) coins = 1;
[coinsLabel setText:[NSString stringWithFormat:#"%d", coins]];
}
else {
[_wrongone setHidden:NO];
score = MIN(score -5, 0);
[scoreLabel setText:[NSString stringWithFormat:#"score: %d", score]];
// animation that shakes the image (qimage)
closeonechange.text = #"Correct!";
}
The label is called
closeonechange.text = "Correct!";
How will I call it to show when the score is below 100, so 99 & below must show the "correct!" label?
Try this,
if([answer isEqualToString:#""]){
...
}
else {
...
}
if (score < 100) {
closeonechange.text = "Correct!";
} else {
closeonechange.text = //set else condition text
}
In your if else statement hide/show it:
if ([answer isEqualToString:#"q"]) { //Score >100 display perfect
// Your code...
closeonechange.text = "Perfect";
}
else { //Score < 100 display correct
// Your code...
closeonechange.text = "Correct";
}
//Extended
If you use just one label to display text instead of hide/show just change the text, see edited above.

NSTimer Milliseconds countdown

I would like to make a simple countdown with seconds and millisecond: SS:MM.
However, i would like to stop the timer or do something when the timer reach 0:00.
Currently the timer works, but it doesnt stop at 0:00. I can make the seconds stop, but not the milliseconds. What is wrong?
-(void) setTimer {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = 99;
tmp.secondsCount = 2;
tmp.countdownTimerGame = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:#selector(timerRun) userInfo:nil repeats:YES];
}
-(void) timerRun {
MySingletonCenter *tmp = [MySingletonCenter sharedSingleton];
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
[tmp.countdownTimerGame invalidate];
tmp.countdownTimerGame = nil;
tmp.lives = tmp.lives - 1;
NSString *newLivesOutput = [NSString stringWithFormat:#"%d", tmp.lives];
livesLabel.text = newLivesOutput;
if (tmp.lives == 0) {
[self performSelector:#selector(stopped) withObject:nil];
}
else {[self setTimer]; }
}
else
tmp.milisecondsCount = 99;
}
NSString *timerOutput = [NSString stringWithFormat:#"%2d:%2d", tmp.secondsCount, tmp.milisecondsCount];
timeLabel.text = timerOutput;
}
-(void) stopped {
NSLog(#"Stopped game");
timeLabel.text = #"0:00";
}
Well. You do
tmp.milisecondsCount = tmp.milisecondsCount - 1;
if(tmp.milisecondsCount == 0){
tmp.milisecondsCount = 100;
tmp.secondsCount -= 1;
}
And right after that
if ((tmp.secondsCount == 0) && tmp.milisecondsCount == 0) {
//stuff
}
How could it ever happen that they're both 0 if, as soon as milisecond reaches 0, you reset it to 100?
EDIT: Do instead something like:
if(tmp.milisecondsCount < 0){
tmp.secondsCount -= 1;
if (tmp.secondsCount == 0){
//Stuff for when the timer reaches 0
//Also, are you sure you want to do [self setTimer] again
//before checking if there are any lives left?
}
else
tmp.milisecondsCount = 99;
}
In your code, a first condition is met
if(tmp.milisecondsCount == 0){
tmp.milisecondsCount = 100;
so that the next conditional statment
&& tmp.milisecondsCount == 0
will never be true.

Problems with view controllers going into the background of UITabBarController

I have a UITabBarController with 3 view controllers connected to it. Everything works fine apart from when you switch from one view to another, the new view takes a moment to reload what it was doing. I know this isn't such a big deal but it just makes the app look a bit sloppy. Does anyone know how I can keep the app running in the background, or something so that it doesn't have to reload each time.
As you might have noticed I'm very new to Objective-C so I can understand if I'm being unclear but any help is really appreciated!
EDIT: FOR DAVID
This is the code for the stopwatch in the .m file:
#implementation StopwatchViewController
- (BOOL)prefersStatusBarHidden
{
return YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
isCounting = false;
}
- (IBAction)startOrStop:(id)sender
{
if (self->isCounting == false) {
self->isCounting = true;
[self startStopwatch];
} else {
self->isCounting = false;
[self stopStopwatch];
}
}
- (void)startStopwatch
{
[startStopButton setTitle:#"STOP" forState:UIControlStateNormal];
[self performSelector:#selector(stopwatch) withObject:self afterDelay:1.0];
}
- (IBAction)resetStopwatch:(id)sender
{
[self reset];
}
- (void)stopwatch
{
if (self->isCounting == false) {
return;
} else {
NSInteger hourInt = [hourLabel.text intValue];
NSInteger minuteInt = [minuteLabel.text intValue];
NSInteger secondInt = [secondLabel.text intValue];
if (secondInt == 59) {
secondInt = 0;
if (minuteInt == 59) {
minuteInt = 0;
if (hourInt == 23) {
hourInt = 0;
} else {
hourInt += 1;
}
} else {
minuteInt += 1;
}
} else {
secondInt += 1;
}
NSString *hourString = [NSString stringWithFormat:#"%02d", hourInt];
NSString *minuteString = [NSString stringWithFormat:#"%02d", minuteInt];
NSString *secondString = [NSString stringWithFormat:#"%02d", secondInt];
hourLabel.text = hourString;
minuteLabel.text = minuteString;
secondLabel.text = secondString;
CGRect hourFrame = self->hourBar.frame;
CGRect minuteFrame = self->minuteBar.frame;
CGRect secondFrame = self->secondBar.frame;
if ((NSInteger)hourFrame.size.height != hourInt) { // check if we need to modify
hourFrame.origin.y -= ((hourInt * 10.0) - hourFrame.size.height);
hourFrame.size.height = (hourInt * 10.0);
self->hourBar.frame = hourFrame;
}
if ((NSInteger)minuteFrame.size.height != minuteInt) { // check if we need to modify
minuteFrame.origin.y -= ((minuteInt * 4.0) - minuteFrame.size.height);
minuteFrame.size.height = (minuteInt * 4.0);
self->minuteBar.frame = minuteFrame;
}
if ((NSInteger)secondFrame.size.height != secondInt) { // check if we need to modify
secondFrame.origin.y -= ((secondInt * 4.0) - secondFrame.size.height);
secondFrame.size.height = (secondInt * 4.0);
self->secondBar.frame = secondFrame;
}
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(stopwatch) userInfo:nil repeats:NO];
}
}
- (void)stopStopwatch
{
[startStopButton setTitle:#"START" forState:UIControlStateNormal];
}
- (void)reset
{
[startStopButton setTitle:#"START" forState:UIControlStateNormal];
self->isCounting = false;
hourLabel.text = #"00";
minuteLabel.text = #"00";
secondLabel.text = #"00";
CGRect hourFrame = self->hourBar.frame;
CGRect minuteFrame = self->minuteBar.frame;
CGRect secondFrame = self->secondBar.frame;
hourFrame.size.height = 0;
minuteFrame.size.height = 0;
secondFrame.size.height = 0;
hourFrame.origin.y = 321.0;
minuteFrame.origin.y = 321.0;
secondFrame.origin.y = 321.0;
self->hourBar.frame = hourFrame;
self->minuteBar.frame = minuteFrame;
self->secondBar.frame = secondFrame;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
SECOND EDIT FOR DAVID:
Changed the main parts of the code to look like this:
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:YES];
[self swapFrames];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self updateBars];
}
- (void)stopwatch
{
if (self->isCounting == false) {
return;
} else {
hourInt = [hourLabel.text intValue];
minuteInt = [minuteLabel.text intValue];
secondInt = [secondLabel.text intValue];
if (secondInt == 59) {
secondInt = 0;
if (minuteInt == 59) {
minuteInt = 0;
if (hourInt == 23) {
hourInt = 0;
} else {
hourInt += 1;
}
} else {
minuteInt += 1;
}
} else {
secondInt += 1;
}
NSString *hourString = [NSString stringWithFormat:#"%02d", hourInt];
NSString *minuteString = [NSString stringWithFormat:#"%02d", minuteInt];
NSString *secondString = [NSString stringWithFormat:#"%02d", secondInt];
hourLabel.text = hourString;
minuteLabel.text = minuteString;
secondLabel.text = secondString;
[self swapFrames];
[self updateBars];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(stopwatch) userInfo:nil repeats:NO];
}
}
- (void)updateBars
{
if ((NSInteger)hourFrame.size.height != hourInt) { // check if we need to modify
hourFrame.origin.y -= ((hourInt * 10.0) - hourFrame.size.height);
hourFrame.size.height = (hourInt * 10.0);
self->hourBar.frame = hourFrame;
}
if ((NSInteger)minuteFrame.size.height != minuteInt) { // check if we need to modify
minuteFrame.origin.y -= ((minuteInt * 4.0) - minuteFrame.size.height);
minuteFrame.size.height = (minuteInt * 4.0);
self->minuteBar.frame = minuteFrame;
}
if ((NSInteger)secondFrame.size.height != (secondInt * 4.0)) { // check if we need to modify
secondFrame.origin.y -= ((secondInt * 4.0) - secondFrame.size.height);
secondFrame.size.height = (secondInt * 4.0);
self->secondBar.frame = secondFrame;
}
}
- (void)swapFrames
{
hourFrame = self->hourBar.frame;
minuteFrame = self->minuteBar.frame;
secondFrame = self->secondBar.frame;
}
I separated the code so that just before the view appear it should update the bars. However, it did not work. I did some investigating by printing out the values of some of the variables at certain points. It appears that in viewWillAppear, secondBar.frame (and minuteBar, etc.) has updated to the correct height. However, in viewDidAppear, that value is reset to 0. This does not happen to secondInt, or secondFrame. Somewhere between those two methods the frame is reset but I cannot figure it out.
From what I understand, the frames for your properties self.hourBar, self.minuteBar and self.secondBar are set to 0 when you switch between tabs, and only update them every second.
If this is indeed the case, just set them to their correct values in your viewControllers viewWillAppear: method (assign them to some property in viewWillDisappear:).
As a sidenote, you seem to be coming from C++. The "->" notation is very uncommon for Objective-C, since properties are accessed with ".", and their corresponding instance variable with "->". Using the arrow notation will not call the auto-synthesised getter/setter methods of properties!
Also, is there a specific reason why you always create new NSTimer objects instead of setting repeats: to yes? Creating a timer and adding it to a runloop (which scheduledTimerWith:... does) is a relatively costly operation.

Resources