zoukankan      html  css  js  c++  java
  • 10.cocos2dx C++为Sprite添加触摸事件监听器

    1.首先头文件定义事件处理的函数原型

    1 private:
    2     bool onTouchBegan(Touch* tTouch,Event* eEvent);//手指按下事件
    3     void onTouchMoved(Touch* tTouch,Event* eEvent);//手指移动事件
    4     void onTouchEnded(Touch* tTouch,Event* eEvent);//手指离开事件

    2.实现原型

     1 bool ShopItem::onTouchBegan(Touch* tTouch,Event* eEvent){
     2  if (sprite->getBoundingBox().containsPoint(tTouch->getLocation())){//判断触摸点是否在目标的范围内
     3  
     4     /**这里为事件内容**/
     5     return true;
     6  }else
     7     return false;
     8  } 
     9 }
    10  
    11 void ShopItem::onTouchMoved(Touch* tTouch,Event* eEvent){
    12     /**这里为事件内容**/
    13 }
    14  
    15 void ShopItem::onTouchEnded(Touch* tTouch,Event* eEvent){
    16     /**这里为事件内容**/
    17 }

    3.绑定事件

    1     auto listener = EventListenerTouchOneByOne::create();
    2     listener->onTouchBegan = CC_CALLBACK_2(ShopItem::onTouchBegan, this);
    3     listener->onTouchMoved = CC_CALLBACK_2(ShopItem::onTouchMoved, this);
    4     listener->onTouchEnded = CC_CALLBACK_2(ShopItem::onTouchEnded, this);
    5     this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sprite);

      

  • 相关阅读:
    Windows 网络监测ping IP输出时间
    python
    遇见问题汇总
    在路上积累
    Condition
    ReentrantReadWriteLock
    AbstractQueuedSynchronizer
    jmeter使用
    使用VisualVM监控java进程
    CNVD漏洞证书(2)
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8350601.html
Copyright © 2011-2022 走看看