Cocos2d-x Touch Detection

I am using Cocos2d-x and trying to detect strokes in my HelloWorld project. Although Iā€™m out of luck.

.h

class HelloWorld : public CCLayer{ private: CCSpriteBatchNode * _batchNode; CCSprite *_turkey; virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event); 

.ccp

 void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event){ CCLog("this"); } 

but the fact is that when you tap the screen, this never appears in the log. What am I missing here?

thanks!

Edit

I am using this tutorial. http://www.raywenderlich.com/11338/cocos2d-x-for-ios-and-android-space-game

+6
source share
6 answers

You need to register with CCTouchDispatcher to get the touch:

Write this in your init() method to get the finishing touches:

 CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, 0); 

In addition, I recommend that you receive the touch event using the touch delegate target methods:

 virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); 

For these methods to be called, you need to register with the touch manager a little differently:

 CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); 

EDIT

In the new cocos version, CCTouchDispatcher is located in CCDirector :

It should look something like this:

 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 
+20
source

So, something super simple, just added

this->setIsTouchEnabled(true);

for my init (); function.

+7
source
 'this' never shows up in the log 
Tips

Perhaps you are using a different version of the Cocos2D library. Go to cocos2d.h in your project and confirm. (The sample was written on 1.0.1). If you are using a different version (guess), you may need to use a different ccTouchesBegan signature and / or fix more than just setIsTouchEnabled to make it work. I just downloaded the sample and calling ccTouchesBegan works ccTouchesBegan fine - without any changes.

+2
source

Here, in the next method, I apply touch to Sprite, if you want to apply the touch event to TextField, Node, Background or any component, just pass this ComponentType to this method and it will work ...

OK LETS BEGIN !!!!

 void YourClassName::YourListnerMethodName(cocos2d::Sprite* object) { auto listener = cocos2d::EventListenerTouchOneByOne::create(); listener->setSwallowTouches(false); listener->onTouchBegan = [=](cocos2d::Touch* touch, cocos2d::Event* event) { auto target = event->getCurrentTarget(); Point locationInNode = target->convertToNodeSpace(touch->getLocation()); // Suppose your sprite or any component is inside in any parent object then use this line instead of above line ... //just uncomment below line and it will work fine in this case //Point locationInNode = target->getParent()->convertToNodeSpace(touch->getLocation()); if (target->getBoundingBox().containsPoint(locationInNode)) { // CODE FOR RESPONSE AFTER TOUCH return true; } return false; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, object); } 

Here, the goal is your component in which you want to apply a touch to it.

Just remember to call this method from ctor or anywhere as per your requirement

0
source

this->setTouchEnabled(true); works better than CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); unfortunately my ccTouchMoved doesn't pick anything up ... :(

-1
source

for cocos2d-x v3.0 ..

write it in the .h 'file

 {bool onTouchBegan (cocos2d::Touch * touch, cocos2d::Event * event);} 

write this in your ' init()' function ..

 { auto listner = EventListenerTouchOneByOne::create(); listner->setSwallowTouches(true); listner->onTouchBegan = CC_CALLBACK_2(Gameplay::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listner, this); } 

and write it in the .cpp file.

 bool "YOURCLASSNAME"::onTouchBegan(cocos2d::Touch* touch, cocos2dEvent* event) { CCLOG("this"); return true; } 
-1
source

Source: https://habr.com/ru/post/918670/


All Articles