• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

cocos2dx3.0的触摸优先和触摸mask问题

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0811/596.html

 
 
游戏开发运营一个实战的例子吧
 
先贴代码
 
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
    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();  
    Point origin = Director::getInstance()->getVisibleOrigin();  
//这里增加了一个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));  
  
  
    // 这里常州手游开发增加了一个背景,下面会用到  
    auto background = Sprite::create("background.png");  
    background->setTextureRect(CCRectMake(0, 0, visibleSize.width, visibleSize.height));  
    background->setContentSize(visibleSize);  
    background->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
    this->addChild(background, 0);  
  
    auto menu = Menu::create(continueItem, NULL);  
    menu->setPosition(Point::ZERO);  
    this->addChild(menu, 1);  
    _eventDispatcher->addEventListenerWithFixedPriority(listener1,-128); // 重点即是这两行,下面会有详解  
    //_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,background);  
 
 
上头那段代码实在也有良多注释了,关于触摸屏障主要完成3个处所
 
[cpp] view plaincopy在常州游戏开发培训CODE上查看代码片派生到我的代码片
listener1->setSwallowTouches(true);//配置不准想下传递触摸    
//如下两段代码二选一  
//第一段是能够解放配置触摸优先级  
//不过不绑定任何器械  
_eventDispatcher->addEventListenerWithFixedPriority(listener1,-128);  
      
//这一段是配置触摸绑定到精灵,优先级为默许的0  
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1,background);  
 
 
另有一点值得留意的是,触摸优先级越低的越先被触摸。手机App外包相像优先级的,后增加的先被触摸
 

上篇:上一篇:U3D用EasyTouch虚拟摇杆控制人物挪动
下篇:下一篇:Cocos2d创建通用UI