1
\$\begingroup\$

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

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

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

@Override
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;
}
\$\endgroup\$
5
  • \$\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
    Commented Aug 22, 2011 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
    Commented Aug 22, 2011 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
    Commented Aug 24, 2011 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
    Commented Aug 24, 2011 at 6:57
  • \$\begingroup\$ There is any other mothod for physics engine . I am new in game development. \$\endgroup\$
    – jubin
    Commented Aug 24, 2011 at 7:43
-2
\$\begingroup\$

http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d

\$\endgroup\$
1
  • 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
    Commented Aug 21, 2011 at 19:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .