c++spritecocos2d-xccspritetouchesbegan

Unable to get touches in COCOS 2dx?


Below is my HelloWorld.h class:

class HelloWorld : public cocos2d::CCLayer
{
public:
HelloWorld();

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

b2World* world;

// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);

virtual void draw();
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void update(float dt);


};

And in my HelloWorld.cpp class i have initialized My init method

bool HelloWorld::init(){
setTouchEnabled( true );
setAccelerometerEnabled( true );
scheduleUpdate();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
return true;
}

This code is working for me now! :)


Solution

  • The Targeted Delegate is for single touch events. Change your events to something like this:

    virtual bool ccTouchBegan (CCTouch *pTouch, CCEvent *pEvent)

    You can read up more about targeted and standard touch delegates on the iPhone side of the Cocos2D documentation at this Link

    Writing the delegate in my initialisation method as per below solved the problem

        CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 1);