How can I handle a touch event in cocos2d for Android? And how to move a sprite using ccTouchesMoved
?
2 Answers
\$\begingroup\$
\$\endgroup\$
5
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;
}
-
\$\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\$– jubinCommented 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\$– bummzackCommented 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\$– jubinCommented 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\$– bummzackCommented Aug 24, 2011 at 6:57
-
\$\begingroup\$ There is any other mothod for physics engine . I am new in game development. \$\endgroup\$– jubinCommented Aug 24, 2011 at 7:43
\$\begingroup\$
\$\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\$– bummzackCommented Aug 21, 2011 at 19:58