Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a method that starts my game (startGame). I call it in viewDidLoad ([self startGame];). The problem is, that the start game method checks to see if an object has moved before it starts the game. The setup I have right now only calls the method once and doesn't check it again which means my game never starts. Any ideas on how I can continue checking the method UNTIL the game starts, and then stop checking it?
Thanks. Happy 2012
there are a few things you can do.. one of the easiest would be to use a NSTimer. so in your viedDidLoad method you could do:
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(checkObjectMoved:) userInfo:NULL repeats:YES]; // check every 1 second
then you'd have:
- (void)checkObjectMoved:(NSTimer *)timer {
if object has NOT moved return;
[timer invalidate];
[self startGame];
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
parallax.update(currentTime)
}
How can i control the update to be done once over a specific period of time
Your code doesn't add much to your question, so I don't really know if this is what you are looking for, but here is some code to call a function every X time (swift 3):
// call MyMethod every 1 second :
Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(MyController.myMethod), userInfo: nil, repeats: true)
Please search before you post a question -> answer
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I am new to programming, but I can't seem to get this to work.
When I try to run the method from another method every thing stops
-(void)rotateMmovment {
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
First you should check either your method is running or not via using NSLog.its does't seems that you are facing problem due to calling method which have empty body
-(void)rotateMmovment {
NSLog(#"My method is running");
}
-(void)stickMove {
[self rotateMmovment];
stick.center = CGPointMake(stick.center.x + x, stick.center.y);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a location app that already successfully asks the user to enable location services, and then can show them their coordinates on a button press as well. So I decided to play around with everything that is available in the CLLocationManager reference provided by xcode.
I decided to setup a bool method called "locationServicesEnabled". It returns a value of YES(1) or NO(0). I declared the method and then went to implement it. I am trying to have NSLog print the bool result out to the console when you open the app.
Here is how I declared the BOOL method in my ViewController.m file:
#interface ViewController () <CLLocationManagerDelegate>
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
+ (BOOL)locationServicesEnabled;
#end
And here is how I implemented the BOOL method in ViewController.m:
+ (BOOL)locationServicesEnabled{
[self locationServicesEnabled];
NSLog(#"%hhd", self.locationServicesEnabled);
return 0;
}
Why do you want to create an extra method? You could minimize the risk of errors by using already available ones: NSLog(#"Location services enabled: %d",[CLLocationManager locationServicesEnabled]);
I think your issue might be the fact that you are calling your function inside its self:
+ (BOOL)locationServicesEnabled
{
[self locationServicesEnabled]; <- should this be here
NSLog(#"%hhd", self.locationServicesEnabled);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to set a timer with a constantly changing delay which is stored in a variable. How would I do this, if I set a timer within a timer to update the delay it doesn't work at all.
Thanks
Don't have a repeating timer. Use a run once timer, and when it completes/runs, create another timer with the new delay.
Or... as #Steve Wilford noted in his answer, use setFireDate: on NSTimer. From the docs "Adjusting the firing time of a single timer would likely incur less expense than creating multiple timer objects, scheduling each one on a run loop, and then destroying them."
In your class interface use
#interface YourClass : SuperClass <Delegates> {
NSTimer *yourTimer;
NSInteger delay;
}
And when you set your timer make sure you set
delay = 1.0;
yourTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:#selector(methodToCall) userInfo:nil repeats:false];
And finally in your methodToCall
-(void)methodToCall {
// Do stuff and reset timer with delay variable
yourTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:#selector(methodToCall) userInfo:nil repeats:false];
}
This means that the timer will have to run down to start running again with a new delay so this might not exactly be the solution you are after if you want something more instantaneous, but changing delay will mean that when the timer runs down it will begin again with your new delay
This also still allows you to call [yourTimer invalidate] and not have repeating timers with no pointer
Hope that helps, or maybe will point you to the solution you are looking for
Maybe the setFireDate: method of NSTimer will help?
Note that it is a "relatively expensive" operation so you should consider if this is actually what you want to be doing.
bandejapaisa has the right idea, but you might need to create the new timer from the main thread. Use:
[self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>]
to create the new timer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do i keep the UILabel updated as the time passes when a post is posted in ios ..like if >its posted just now it should say 0s and as time passes it changes to >1min,2min...1hr,2hr...1day and so in ios
I am new to the ios development
Thanks for any help!
You need to use NSTimer for this purpose if I understand your questions properly.
Doing something on a regular interval is pretty easy in iOS.
In your main code:
[NSTimer scheduledTimerWithTimeInterval: 1.0
target: self
selector: #selector(doSomething:)
userInfo: nil
repeats: YES];
Then add the doSomething method:
- (void)doSomething:(NSTimer *)timer {
NSLog(#"We did it!");
}
First of all you need to keep the date when user posted. You can easily do that by using:
NSDate* postedDate = [NSDate now];
Than if you want to find a seconds left from that time you can use:
NSTimeInterval timeDiff = [[NSDate date] timeIntervalSinceDate:postedDate];
This gives you seconds from posting date - you need to format output.
For keeping label updating itself you should use NSTimer:
[NSTimer scheduledTimerWithTimeInterval: 1.0f
target: self
selector: #selector(updateLabel:)
userInfo: nil
repeats: YES];
Where updateLabel will format "timeDiff" into something human readable like 1minute / day etc...