zoukankan      html  css  js  c++  java
  • cocos2d-x:解决点击拖动按钮后,所在的layer监听不到触摸事件的问题

    点击拖动按钮后,想让所在的layer监听到屏幕的触摸事件,可以把该按钮拖动开始的时候设置setTouchEnabled为false;
    例:

    #include "ui/UIButton.h"
    
    bool myDemo::init() {
    
      
    
      // 设置点击事件
      EventListenerTouchOneByOne *listener = EventListenerTouchOneByOne::create();
      listener->onTouchBegan = CC_CALLBACK_2( myDemo::onTouchBegan, this );
      listener->onTouchMoved = CC_CALLBACK_2( myDemo::onTouchMoved, this );
      listener->onTouchEnded = CC_CALLBACK_2( myDemo::onTouchEnded, this );
    
    
      // 绑定
      getEventDispatcher()->addEventListenerWithSceneGraphPriority( listener, this );
    
      
    
      // 按钮创建
      Button *btn = ***;      
      btn ->addTouchEventListener( CC_CALLBACK_2( myDemo::BtnCallBack, this ) );
    
      this->addChild( btn  );
      return true;
    }
    
    void myDemo::BtnCallBack( Ref *p, cocos2d::ui::Widget::TouchEventType type ) {
    
      Button *btn = (Button *)p; // 
    
      if ( type == Widget::TouchEventType::BEGAN ) {
        btn->setTouchEnabled( false );
    
      }
    
    }
    
     
    
    bool myDemo::onTouchBegan( cocos2d::Touch *touch, cocos2d::Event *event ) {
    
      return true;
    
    }
    
     
    
    void myDemo::onTouchMoved( cocos2d::Touch *touch, cocos2d::Event *event ) {
    
    
    }
    
     
    
    void myDemo::onTouchEnded( cocos2d::Touch *touch, cocos2d::Event *event ) {
    
    
    }

    //当拖动按钮时,会进入onTouchMoved方法

  • 相关阅读:
    Deployment descriptor
    实体、list 、xml之间的转化
    关于C# 汉字转拼音问题
    NPoco学习笔记(1)
    SQL(二)
    SQL(一)
    sobel算子及cvSobel
    图像的平滑处理
    erase的用法
    int main(int argc, char* argv[ ])
  • 原文地址:https://www.cnblogs.com/Colored-Mr/p/4240233.html
Copyright © 2011-2022 走看看