I am using Xcode 5.1 and Cocos2d v3.0 for reference. In this practice app I want the user to hold down on the screen. If the user holds down for one second the program should run "someMethod". Every second the user holds down on the screen the program should run "someMethod". So if the user holds down on the screen for a total of five second at each internal of one second "someMethod" should be called. If the user isn't holding down on the screen the time shouldn't run. I want this to work each timer the user holds down. So if the user holds down for one second, removes finger from screen, and then holds down again the timer should reset. Finally if the user holds down for 2.5 seconds "someMethod" should not be fired for a third time.
My problem is that my repeating timer isn't stopping if I lift my finger from the screen
Here is an example of my output
2014-12-03 19:47:45.987 Practice App[14739:f03] fire 2014-12-03 19:47:46.986 Practice App[14739:f03] fire //after this point in time I am not holding down on the screen 2014-12-03 19:47:47.986 Practice App[14739:f03] fire 2014-12-03 19:47:48.986 Practice App[14739:f03] fire 2014-12-03 19:47:49.986 Practice App[14739:f03] fire
@implementation GameScene
{
dispatch_source_t dispatchSource;
}
- (instancetype)init
{
if (self = [super init]){
dispatchSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));
double interval = 1.0;
dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, 0);
uint64_t intervalTime = (int64_t)(interval * NSEC_PER_SEC);
dispatch_source_set_timer(dispatchSource, startTime, intervalTime, 0);
dispatch_source_set_event_handler(dispatchSource, ^{
[self someMethod];
});
...
}
return self;
}
- (void)fixedUpdate:(CCTime)dt
{
if(touchState == kTouchDown){//The player is touching the screen
dispatch_resume(dispatchSource);
}
if (touchState == kTouchUp) {//The player isn't touching the screen
dispatch_suspend(dispatchSource);
}
}
- (void)someMethod{
NSLog(@"fire");
}