【问题标题】:Cocos2d-x 3.0rc not detecting touchesCocos2d-x 3.0rc 没有检测到触摸
【发布时间】:2014-04-01 16:39:02
【问题描述】:

在新的 cocos2d-x 3.0rc 中,我想检测图层中的触摸。如下所述,我在班级中有过度使用的功能。

virtual bool onTouchBegan(CCTouch* touch, CCEvent* event);
virtual void onTouchMoved(CCTouch* touch, CCEvent* event);
virtual void onTouchEnded(CCTouch* touch, CCEvent* event);

但未检测到触摸。知道这是怎么回事吗?

【问题讨论】:

    标签: cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    为了启用触摸,我使用了以下代码。在 cocos2d-x 3.0 RC1 中完美运行

    void class_name::onEnter()
    {
        Layer::onEnter();
    
        // Register Touch Event
        auto dispatcher = Director::getInstance()->getEventDispatcher();
        auto listener = EventListenerTouchOneByOne::create();
    
        listener->onTouchBegan = CC_CALLBACK_2(class_name::onTouchBegan, this);
        listener->onTouchMoved = CC_CALLBACK_2(class_name::onTouchMoved, this);
        listener->onTouchEnded = CC_CALLBACK_2(class_name::onTouchEnded, this);
    
        dispatcher->addEventListenerWithSceneGraphPriority(listener, this);
    }
    

    【讨论】:

    • 和 onexit 这样的方法 void GameScene::onExit() { auto dispatcher = Director::getInstance()->getEventDispatcher();自动监听器 = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); dispatcher->removeEventListener(listener);层::onExit(); } ?
    【解决方案2】:

    在 Cocos2d-x 3.0 中检测触摸

    在 (HelloWorld.h) 中编写代码 {

    cocos2d::EventListenerTouchAllAtOnce *Listner;
    void onTouchesBegan(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event);
        void onTouchesMoved(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event);
        void onTouchesEnded(const std::vector<cocos2d::Touch *> &touches, cocos2d::Event *event);
    

    }

    在init Method(HelloWorld.cpp)中编写代码 {

     Listner = EventListenerTouchAllAtOnce::create();
        Listner->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
        Listner->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this);
        Listner->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(Listner, this);
    

    }

    【讨论】:

      【解决方案3】:
      enable touch on init() Or onEnter()
      
      this->setTouchEnabled(true);
      CCDirector::sharedDirector() -> getTouchDispatcher() -> addTargetedDelegate( this, 0, true );
      

      【讨论】:

      • @KARTHIK 我猜这现在在 cocos2d-x 3.0 中不起作用。我得到了一个完美运行的代码块。检查一下,我自己回答了这个问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多