实习小白::cocos2d-x 2.2 study ---------- 长按事件处理

 

cocos2d-x 2.2 study    ----------    长按事件处理

 

其实是在点击事件发生的时候,

在开始的时候添加一个一次的计时器,

在计时器中添加方法,

在结束的时候将添加的内容移除掉

 

代码:(主要代码)

 1 void    GetFirstRechargePackLayer::menuEvent(CCObject* pSender, cocos2d::ui::TouchEventType type)
 2 {
 3     switch (type)
 4     {
 5     case TOUCH_EVENT_BEGAN:
 6         {
 7             if ( pSender == m_pImage4)
 8                 m_materialType = EP_HERO;
 9             else
10                 m_materialType = EP_ITEM;
11             m_isLongClick = false;
12             m_icon = pSender;
13             touchBeginPoint = ((cocos2d::ui::UIImageView*)pSender)->getTouchStartPos();
14             this->scheduleOnce(schedule_selector (GetFirstRechargePackLayer::createTipLayer),0.2);
15         }
16         break;
17     case TOUCH_EVENT_MOVED:
18         {
19             touchMovedPoint = ((cocos2d::ui::UIImageView*)pSender)->getTouchMovePos();
20             float distance = sqrt((touchMovedPoint.x-touchBeginPoint.x) * (touchMovedPoint.x-touchBeginPoint.x) + (touchMovedPoint.y-touchBeginPoint.y)*(touchMovedPoint.y-touchBeginPoint.y));
21 
22             if (distance >= 50)
23             {
24                 this->unschedule(schedule_selector (GetFirstRechargePackLayer::createTipLayer));
25             }
26         }
27         break;
28     case TOUCH_EVENT_ENDED:
29         {
30             this->unschedule(schedule_selector (GetFirstRechargePackLayer::createTipLayer));
31             if (m_isLongClick == true)
32             {
33                 this->removeChild(m_pLayerTips);
34             }
35         }
36         break;
37     case TOUCH_EVENT_CANCELED:
38         {
39             this->unschedule(schedule_selector (GetFirstRechargePackLayer::createTipLayer));
40             if (m_isLongClick == true)
41             {
42                 this->removeChild(m_pLayerTips);
43             }
44         }
45         break;
46     default:
47         break;
48     }
49 }
50 
51 void GetFirstRechargePackLayer::createTipLayer(float dt)
52 {
53     m_isLongClick = true;
54     // 参数:1->图片(所点击的那个图片),2->类型(图片的类型),3->id
55     m_pLayerTips = DesriptionTips::create(m_icon,m_materialType,(long)((cocos2d::ui::UIImageView*)m_icon)->getUserData());
56     
57     this->addChild(m_pLayerTips);
58 }

 

posted @ 2015-11-18 19:02  silent-bobo  阅读(242)  评论(0编辑  收藏  举报