下面我们要学习的是cocos2dx 3.0触摸优先级,在这用代码说话。以此可以让我们更加了解cocos2dx 3.0触摸优先级
  1. [cpp]
  2. auto listener1 = EventListenerTouchOneByOne::create();//创建一个触摸监听
  3. listener1->setSwallowTouches(true);//设置不想向下传递触摸  true是不想 默认为false
  4. listener1->onTouchBegan = [](Touch* touch, Event* event)
  5. {
  6. CCLOG("touch menu");
  7. return true;
  8. };
  9. listener1->onTouchMoved = [](Touch* touch, Event* event)
  10. {
  11. };
  12. listener1->onTouchEnded = [=](Touch* touch, Event* event)
  13. {
  14. };
  15. Size visibleSize = Director::getInstance()->getVisibleSize();
  16. "white-space:pre">  Point origin = Director::getInstance()->getVisibleOrigin();
  17. "white-space:pre">  //这里添加了一个MenuItem 用来对比触摸优先级
  18. auto continueItem = MenuItemImage::create(
  19. "ContinueNormal.png",
  20. "ContinueSelect.png",
  21. CC_CALLBACK_1(MenuLayer::menuContinueCallback, this));
  22. continueItem->setPosition(Point(origin.x + visibleSize.width/2  ,
  23. origin.y + visibleSize.height/2+continueItem->getContentSize().height));
  24. // 这里添加了一个背景,下面会用到
  25. "white-space:pre">  auto background = Sprite::create("background.png");
  26. background->setTextureRect(CCRectMake(0, 0, visibleSize.width, visibleSize.height));
  27. background->setContentSize(visibleSize);
  28. "white-space:pre">  background->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
  29. "white-space:pre">  this->addChild(background, 0);
  30. "white-space:pre">  auto menu = Menu::create(continueItem, NULL);
  31. "white-space:pre">  menu->setPosition(Point::ZERO);
  32. "white-space:pre">  this->addChild(menu, 1);
  33. _eventDispatcher->addEventListenerWithFixedPriority(listener1,-128); // 重点就是这两行,下面会有详解
  34. //_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,background);
还有一点值得注意的是,触摸优先级越低的越先被触摸。相同优先级的,后添加的先被触摸