1

I have created coding for single and double click. I want to prevent single click event when making double click.

When making single click, the shape will changed circle to rectangle and vice versa.

When making double click, the shape is destroyed.

While making double click, the single click event is also executed. I want to prevent single click event at that time.

Any idea should help me.

Here the coding

void HelloWorld::ccTouchesEnded(CCSet *touch, CCEvent *pEvent)
{
   CCTouch* myTouch = (CCTouch*)touch->anyObject();
   CCTime::gettimeofdayCocos2d(&endTime, NULL);
   duration = CCTime::timersubCocos2d(&startTime, &endTime); //duration of tap in milliseconds
   finalPosition = myTouch->getLocation();
   float distance = distanceBetweenPoints(finalPosition,initialPosition);

   if (duration<kTapMaxDuration && distance <kTapMaxDistance)
   {
      taps++;
   }
   else
     taps=0;

    CCPoint touchLocation=myTouch->locationInView();
touchLocation=CCDirector::sharedDirector()->convertToGL(touchLocation);
    CCPoint nodePosition = this->convertToNodeSpace(touchLocation);
this->mouseUp(b2Vec2(nodePosition.x/kPhysicsPTMRatio,nodePosition.y/kPhysicsPTMRatio));
    CCTime::gettimeofdayCocos2d(&startTime, NULL);
 }

And the mouse up event,

 void HelloWorld::mouseUp(b2Vec2 p)
 {
     b2AABB aabb;
 b2Vec2 d = b2Vec2(0.001f, 0.001f);
 aabb.lowerBound = p - d;
 aabb.upperBound = p + d;

 MyQueryCallback callback(p);

 world_->QueryAABB(&callback, aabb);

     if (callback.m_fixture)
 {
       b2Body *body = callback.m_fixture->GetBody();
       BodyNode *node = (BodyNode*) body->GetUserData();
   if( node && node->isTouchable_ )
       {
        if( (dynamic_cast<Shape1*>(node) != NULL))
        {
            if (taps == 1)
            {
                this->removeB2Body(body);
            }
            else if(taps < 1)
            {
                dynamic_cast<Shape1*>(node)->toggleShape();
            }
        }
    }
}

}

  • 3
    click != touch/tap ... if you develop for iOS, use the tap gesture recognizer: http://stackoverflow.com/questions/8876202/uitapgesturerecognizer-single-tap-and-double-tap There may be an analog for Android. Regardless the principle is the same: when you receive "touch began" then delay running the single tap code until a certain time. If time elapses with no tap ended and a follow-up tap began, then it was a single tap. Otherwise it was a double-tap (two taps within a certain time frame). – CodeSmile Oct 10 '13 at 12:46
  • @LearnCocos2D: thanks for your help. I got idea and implemented my games. –  Oct 11 '13 at 06:18
  • @user2815602 Could you answer your own question for short? I'm having the same problem and my code is not yet working correctly. – blueprint Sep 08 '15 at 05:23

0 Answers0