cocos2dx3.0屏蔽触摸要怎么去解决呢?我还是用一个实战的例子来说明问题吧。
先贴代码
- auto listener1 = EventListenerTouchOneByOne::create();//创建一个触摸监听
- listener1->setSwallowTouches(true);//设置不想向下传递触摸 true是不想 默认为false
- listener1->onTouchBegan = [](Touch* touch, Event* event){
- CCLOG("touch menu");
- return true;
- };
- listener1->onTouchMoved = [](Touch* touch, Event* event){
- };
- listener1->onTouchEnded = [=](Touch* touch, Event* event){
- };
- Size visibleSize = Director::getInstance()->getVisibleSize();
- "white-space:pre"> Point origin = Director::getInstance()->getVisibleOrigin();
- "white-space:pre"> //这里添加了一个MenuItem 用来对比触摸优先级
- auto continueItem = MenuItemImage::create(
- "ContinueNormal.png",
- "ContinueSelect.png",
- CC_CALLBACK_1(MenuLayer::menuContinueCallback, this));
- continueItem->setPosition(Point(origin.x + visibleSize.width/2 ,
- origin.y + visibleSize.height/2+continueItem->getContentSize().height));
- // 这里添加了一个背景,下面会用到
- "white-space:pre"> auto background = Sprite::create("background.png");
- background->setTextureRect(CCRectMake(0, 0, visibleSize.width, visibleSize.height));
- background->setContentSize(visibleSize);
- "white-space:pre"> background->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
- "white-space:pre"> this->addChild(background, 0);
- "white-space:pre"> auto menu = Menu::create(continueItem, NULL);
- "white-space:pre"> menu->setPosition(Point::ZERO);
- "white-space:pre"> this->addChild(menu, 1);
- _eventDispatcher->addEventListenerWithFixedPriority(listener1,-128); // 重点就是这两行,下面会有详解
- //_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,background);
上面那段代码其实也有很多注释了,关于cocos2dx3.0屏蔽触摸主要实现3个地方
- listener1->setSwallowTouches(true);//设置禁止想下传递触摸
- //以下两段代码二选一
- //第一段是可以自由设置触摸优先级
- //但是不绑定任何东西
- _eventDispatcher->addEventListenerWithFixedPriority(listener1,-128);
- //这一段是设置触摸绑定到精灵,优先级为默认的0
- //_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,background);
还有一点值得注意的是,触摸优先级越低的越先被触摸。相同优先级的,后添加的先被触摸。