Question

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! :)

Was it helpful?

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);

OTHER TIPS

If you want to disable multiTouch capability you can use :

virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){}
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){}

And so you need to:

bool init()
{
    ...
    setTouchEnabled(true);
    this->setTouchMode(ccTouchesMode::kCCTouchesOneByOne); // Important
}

If you omit last line you will need to override these (multiTouch mode):

void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top