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方法

  • 相关阅读:
    521.最长特殊序列 I
    520.检查大写字母
    459.重复的子字符串
    Java 读取 .properties 文件的几种方式
    Idea 使用教程
    db2 with用法
    DB2 alter 新增/删除/修改列
    Bootstrap treegrid 实现树形表格结构
    Mysql 递归查询
    navicat for mysql 下载安装教程
  • 原文地址:https://www.cnblogs.com/Colored-Mr/p/4240233.html
Copyright © 2011-2022 走看看