【问题标题】:Detect touch Cocos2d-x检测触摸 Cocos2d-x
【发布时间】:2012-06-21 15:06:27
【问题描述】:

我正在使用 Cocos2d-x 并尝试在我的 HelloWorld 项目中检测触摸。虽然我运气不好。

.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");
}

但问题是,当我单击屏幕时,“this”永远不会出现在日志中。我在这里错过了什么?

谢谢!

编辑,

我正在使用本教程。 http://www.raywenderlich.com/11338/cocos2d-x-for-ios-and-android-space-game

【问题讨论】:

    标签: iphone c++ objective-c cocos2d-iphone cocos2d-x


    【解决方案1】:

    您必须注册 CCTouchDispatcher 才能接收触摸:

    在您的 init() 方法中写入此内容以便接收触摸:

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

    另外我建议您通过有针对性的触摸委托方法接收触摸事件:

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

    为了调用这些方法,您必须向触摸调度程序注册有点不同:

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

    编辑

    在cocos新版本中CCTouchDispatcher位于CCDirector

    它应该看起来像这样:

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

    【讨论】:

    • 嗯,当我在 HelloWorld.cpp 中将它添加到我的 init() 中时,我在 'cocos2d::CCTouchDispatcher' 中收到错误 No memeber named 'sharedDispatcher'
    • @JamesDunay:你用的是什么版本的cocos?
    • 所以现在我得到了错误,Assertion failed: (false), function ccTouchBegan, file /Iphone/SDK/Eyes/Eyes C++/Eyes C++/libs/cocos2dx/layers_scenes_transitions_nodes/CCLayer.cpp, line 292.
    • 我发布了我正在使用的教程,它似乎没有提到任何getTouchDispatcherCCTouchDispatcher,但我能够在他的方法中记录结果
    • 您是否负责在移除层时移除目标委托?
    【解决方案2】:

    所以一些超级简单的东西,只是添加了

    this->setIsTouchEnabled(true);

    到我的 init();功能。

    【讨论】:

      【解决方案3】:
      'this' never shows up in the log
      

      提示您可能正在使用不同版本的 Cocos2D 库。请在您的项目上转到cocos2d.h 并确认。 (样本写在 1.0.1 上)。如果您使用的是不同的版本(猜测),您可能必须使用不同的 ccTouchesBegan 签名和/或修复更多的 setIsTouchEnabled 才能使其工作。我刚刚下载了示例,ccTouchesBegan 调用完美 - 没有任何更改。

      【讨论】:

        【解决方案4】:

        this->setTouchEnabled(true);CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); 工作得更好,不幸的是我的 ccTouchMoved 没有收到任何东西... :(

        【讨论】:

          【解决方案5】:

          对于 cocos2d-x v3.0..

          将其写入您的“.h”文件中

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

          把这个写在你的'init()' function..

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

          并将其写入“.cpp”文件中..

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

          【讨论】:

            【解决方案6】:

            在下面的方法中,我在 Sprite 上应用触摸,如果你想在 TextField 、 Node 、 Background 或任何组件上应用触摸事件,只需将该 ComponentType 传递给此方法,它就会起作用....

            好的,开始吧!!!!

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

            这里的目标是你的组件,你想在它上面应用触摸

            只是不要忘记根据您的要求从 ctor 或任何地方调用此方法

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-09-27
              • 1970-01-01
              相关资源
              最近更新 更多