下面我们要学习的是cocos2dx 3.0触摸优先级,在这用代码说话。以此可以让我们更加了解cocos2dx 3.0触摸优先级。
- [cpp]
- 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);