How can I handle a touch event in cocos2d for Android? And how to move a sprite using ccTouchesMoved?


You need to enable touches by adding this.setIsTouchEnabled(true); to the constructor of your CCLayer. Then override ccTouchesMoved, like this:

public boolean ccTouchesMoved(MotionEvent e)
    // convert event location to CCPoint
    CCPoint p = CCDirector.sharedDirector().convertToGL(e.getX(), e.getY());

    // update the position of your sprite
    yourSprite.setPosition(p.x, p.y);

    return true;
  • \$\begingroup\$ Hello bummzack thanks a lot for helping me it is working. Could you also tell me if i want to move the object with box2d body how can i move ? \$\endgroup\$ – jubin Aug 22 '11 at 2:07
  • \$\begingroup\$ @jubin: You could simply set the position of the Box2d body in the ccTouchesMoved function. Or implement mouse-picking using the MouseJoint like in the Box2d testbed. \$\endgroup\$ – bummzack Aug 22 '11 at 7:13
  • \$\begingroup\$ Thanks for the reply if i want to do this without using mouse joint can i do becoz i don't know how to use the mouse joint. I am new in game development. \$\endgroup\$ – jubin Aug 24 '11 at 1:20
  • \$\begingroup\$ @jubin: If that stuff is too complicated for you, you might want to consider not using a physics-eninge at all and rather implement a simple game without the use of Box2d. \$\endgroup\$ – bummzack Aug 24 '11 at 6:57
  • \$\begingroup\$ There is any other mothod for physics engine . I am new in game development. \$\endgroup\$ – jubin Aug 24 '11 at 7:43


  • 1
    \$\begingroup\$ -1. While this appears to be an excellent tutorial, a link alone is not an answer (what happens if that site goes down?) and also it's about cocos2d for iOS which is different from the android port. \$\endgroup\$ – bummzack Aug 21 '11 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.