Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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;
}
share|improve this answer
    
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 ? –  jubin Aug 22 '11 at 2:07
    
@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. –  bummzack Aug 22 '11 at 7:13
    
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. –  jubin Aug 24 '11 at 1:20
    
@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. –  bummzack Aug 24 '11 at 6:57
    
There is any other mothod for physics engine . I am new in game development. –  jubin Aug 24 '11 at 7:43

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

share|improve this answer
1  
-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. –  bummzack Aug 21 '11 at 19:58
    
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Jonathan Hobbs Aug 22 '12 at 7:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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