In the development process of cocos2d-x sometimes also need to use double-click event processing, so because in the cocos2d-x does not implement double-click event processing, then we need to own code implementation.
The following describes how to process double-click events.
(1) method 1
The first method is to use the interval between the two touch to determine whether a click or double-click. Generally, double-click the touch, and the interval between the two touch is Ms ~ 300 ms (ms ). Therefore, you can use this condition to determine and process the double-click operation.
Note that the following method is required to obtain the touch time between two touch operations in milliseconds:
[Cpp]
// Obtain the system's Millisecond Time
Long millisecondNow ()
{
Struct cc_timeval now;
CCTime: gettimeofdayCocos2d (& now, NULL );
Return (now. TV _sec * 1000 + now. TV _usec/1000 );
}
// Obtain the system's Millisecond Time
Long millisecondNow ()
{
Struct cc_timeval now;
CCTime: gettimeofdayCocos2d (& now, NULL );
Return (now. TV _sec * 1000 + now. TV _usec/1000 );
}
To obtain the time of two touch operations, you need to define two long variables to record the time, and define whether a bool type variable record is double-clicked.
[Cpp]
Long preTime;
Long nextTime;
Bool isClickTwo;
Long preTime;
Long nextTime;
Bool isClickTwo;
Initialization:
[Cpp]
PreTime = 0.0f;
NextTime = 0.0f;
IsClickTwo = false;
PreTime = 0.0f;
NextTime = 0.0f;
IsClickTwo = false;
The next step is to use the ccTouchesBegan (CCSet * touches, CCEvent * event) method (note that this method should be used in began instead of ended) click and double-click. The Code is as follows:
[Cpp]
Void HelloWorld: ccTouchesBegan (CCSet * touches, CCEvent * event)
{
IsClickTwo = false;
NextTime = millisecondNow ();
If (nextTime-preTime <300) & (nextTime-preTime> 50 )){
CCLog ("double click ");
IsClickTwo = true;
}
// Process the click event in the callback Method
ScheduleOnce (schedule_selector (HelloWorld: ff), 0.25f );
PreTime = millisecondNow ();
}
Void HelloWorld: ff (float tt)
{
If (! IsClickTwo ){
CCLog ("single click ");
}
}
Void HelloWorld: ccTouchesBegan (CCSet * touches, CCEvent * event)
{
IsClickTwo = false;
NextTime = millisecondNow ();
If (nextTime-preTime <300) & (nextTime-preTime> 50 )){
CCLog ("double click ");
IsClickTwo = true;
}
// Process the click event in the callback Method
ScheduleOnce (schedule_selector (HelloWorld: ff), 0.25f );
PreTime = millisecondNow ();
}
Void HelloWorld: ff (float tt)
{
If (! IsClickTwo ){
CCLog ("single click ");
}
}
In this way, when you click or double-click the screen, the terminal output will be clicked or double-clicked.
(2) method 2
The following method is more concise than the first method. Instead of directly comparing the time interval between two touch double-clicking, It is a clever solution to this problem. The solution is as follows: we know that the double-click interval is 0.25 ms, so we set a schedule with a delay of ms. If we touch again in the 0.25ms, double-click. Otherwise, click. The specific implementation process depends on the code implementation, which should be understandable.
In this method, you only need to use a bool variable to determine whether to touch.
[Cpp]
Bool clicked;
Bool clicked;
Initialization:
[Cpp]
Clicked = false;
Clicked = false;
The Code is as follows:
[Cpp]
Void HelloWorld: ccTouchesBegan (CCSet * touches, CCEvent * event)
{
If (clicked ){
Clicked = false;
CCLog ("double click ");
}
Else
{
// Execution with a latency of 0.25s (note that the value of clicked is already true during the delay of 0.25s ),
// If you click again in this process, the preceding double-click event will be executed to handle it.
// Otherwise, execute the following callback function to process the Click Event
ScheduleOnce (schedule_selector (HelloWorld: ff), 0.25f );
Clicked = true;
}
}
// Process the click event in the callback function
Void HelloWorld: ff (float tt)
{
If (clicked ){
Clicked = false;
CCLog ("single click ");
}
}
Void HelloWorld: ccTouchesBegan (CCSet * touches, CCEvent * event)
{
If (clicked ){
Clicked = false;
CCLog ("double click ");
}
Else
{
// Execution with a latency of 0.25s (note that the value of clicked is already true during the delay of 0.25s ),
// If you click again in this process, the preceding double-click event will be executed to handle it.
// Otherwise, execute the following callback function to process the Click Event
ScheduleOnce (schedule_selector (HelloWorld: ff), 0.25f );
Clicked = true;
}
}
// Process the click event in the callback function
Void HelloWorld: ff (float tt)
{
If (clicked ){
Clicked = false;
CCLog ("single click ");
}
}
In the same way, the terminal outputs the click or double-click information.
By comparing the implementation methods of the two, we can see that the implementation process of the two is a bit similar, but the implementation process of double-click is slightly different, in actual use, we recommend that you use the second method, which is concise.