首页 > 代码库 > cocos2dx 开发成长之路 002

cocos2dx 开发成长之路 002

在多个layer的情况下,为了避免触摸事件穿透,我们要把触摸事件截取在当前层,具体做法如下

    auto listener = EventListenerTouchOneByOne::create();

    listener->setSwallowTouches(true);

    listener->onTouchBegan = CC_CALLBACK_2(MyLayer::onTouchBegan, this);

    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

 

  

  bool MyLayer::onTouchBegan(Touch *pTouch, Event *pEvent)

 {

    return true;

 }

cocos2dx 开发成长之路 002