Detect touch Cocos2d-x
Asked Answered
C

6

6

I'm using Cocos2d-x and trying to detect touches in my HelloWorld project. Though I'm having no 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 thing is that when I click the screen 'this' never shows up in the log. What am i missing here?

thanks!

Edit,

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

Clerihew answered 21/6, 2012 at 15:6 Comment(0)
V
20

You have to register with CCTouchDispatcher in order to receive touches:

Write this in your init() method in order to receive touches:

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

Also I recommend you to receive touch event via targeted touch delegate 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);

In order these methods to be called you have to register with touch dispatcher a bit different:

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

EDIT

In new cocos version CCTouchDispatcher is located in CCDirector:

It should look something like this:

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
Vernellvernen answered 21/6, 2012 at 15:11 Comment(5)
hmmm i get the error No memeber named 'sharedDispatcher' in 'cocos2d::CCTouchDispatcher' when i ad it to my init(), in HelloWorld.cppClerihew
@JamesDunay: what cocos version do you use?Vernellvernen
So now i get the error, Assertion failed: (false), function ccTouchBegan, file /Iphone/SDK/Eyes/Eyes C++/Eyes C++/libs/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp, line 292.Clerihew
I posted the tutorial that I'm using and it doesnt seem to mention any getTouchDispatcher or CCTouchDispatcher, but i am able to Log results in his methodClerihew
Are you responsible for removing the targeted delegate when the layer is removed?Leaving
C
8

So something super simple, just added

this->setIsTouchEnabled(true);

to my init(); function.

Clerihew answered 21/6, 2012 at 16:38 Comment(0)
G
2
'this' never shows up in the log

hints You might be using a different version of Cocos2D library. Please go to cocos2d.h on your project and confirm. (the sample was written on 1.0.1). If you are on a different version, (guessing) you might have to use different ccTouchesBegan signature and/or fix more than just setIsTouchEnabled to make it work. I just downloaded the sample, and the ccTouchesBegan call works perfect - without any changes.

Gonocyte answered 27/7, 2012 at 0:4 Comment(0)
T
0

this->setTouchEnabled(true); works better than CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);unfortunately my ccTouchMoved is not picking anything up... :(

Tartarean answered 17/4, 2013 at 14:53 Comment(0)
O
0

for cocos2d-x v3.0..

write this in your '.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 this in '.cpp' file..

bool "YOURCLASSNAME"::onTouchBegan(cocos2d::Touch* touch, cocos2dEvent* event)
{   
        CCLOG("this");
             return true;
}
Ola answered 19/6, 2015 at 13:39 Comment(0)
H
0

Here in below method I am applying touch on Sprite , If you want to apply touch event on TextField , Node , Background , or in any component just Pass that ComponentType into 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 target is your component , where you want to apply touch on it

Just don't forget to call this method from ctor or anywhere according to your requirement

Heartwood answered 5/6, 2017 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.