Touch and Hold?

Is there a method to detect a touch and hold event? If not, I’ll post some code when I get something up and running.

Ok, found out how to implement it. Here is how to create a Cocos2d-x touches and hold compatible function:

1.) In the class where the touch and hold code needs to reside, add:
ccTime GlobalTimer;
virtual void TouchesHeldCallback(ccTime deltaTime);

2.) In the ccTouchesBegan function, add:
//reset the timer
GlobalTimer = 0;
//schedule the function that with scheduler
this~~>schedule);
3.) In the ccTouchesEnd and ccTouchesMoved, add the following to cancel the timer:
//unschedule and remove the function from the scheduler
this~~>unschedule(schedule_selector(AvailibleUnits::TouchesHeldCallback));

4.) Put your code in the TouchesHeldCallback(ccTime deltaTime) function:
void MyClass::TouchesHeldCallback(ccTime time){
//delta time is added to the global timer
GlobalTimer+=time;
//when the GlobalTimer is > then the specified time, execute the code
if(GlobalTimer > XSeconds){
//add code here if the user touches and holds for XSeconds
}
}

1 Like

Oops, sorry, forgot one thing:

5.) put this->schedule(schedule_selector(AvailibleUnits::TouchesHeldCallback)); in the if(GlobalTimer > XSeconds){ } if then statement so it doesn’t run more than one time.

1 Like