cocos2d-x 环形滑动控件

前端之家收集整理的这篇文章主要介绍了cocos2d-x 环形滑动控件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. </pre><pre name="code" class="cpp">用于环形滑动,选择英雄
  1. <img src="http://img.blog.csdn.net/20141008203419127?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHl6MTQ1MTc4NDE0NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />
  1. </pre><pre name="code" class="cpp">//MyRoleScrollView.h
  2.  
  3. #pragma once
  4.  
  5. #include "cocos2d.h"
  6. USING_NS_CC;
  7.  
  8. class nodeAndPt:public CCObject
  9. {
  10. public:
  11. nodeAndPt(CCNode *node,int ptindex)
  12. {
  13. this->node = node;
  14. this->ptIndex = ptindex;
  15. }
  16. ~nodeAndPt()
  17. {
  18. }
  19. int ptIndex;
  20. CCNode *node;
  21. };
  22.  
  23. class MyRoleScrollView:public CCNode,public CCTouchDelegate
  24. {
  25. public:
  26. MyRoleScrollView():maxNum(6),isTouchDown(false),isMoveTimeOut(true)
  27. {}
  28. ~MyRoleScrollView()
  29. {
  30.  
  31. }
  32. static MyRoleScrollView * create(CCArray * nodeArr = NULL,int cgm = 60,int radius = 200);
  33. void addRole(CCNode * node);
  34.  
  35. private:
  36. bool init(CCArray * nodeArr,int cgm,int radius);
  37. void initData(CCArray * nodeArr,int radius);
  38. void initView();
  39.  
  40. virtual void onEnter();
  41. virtual void onExit();
  42.  
  43. virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);
  44. virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);
  45. virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);
  46. virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);
  47.  
  48. void setPt(nodeAndPt * p,int i);
  49.  
  50. void refreshPt();
  51.  
  52. void updateOnce(float t);
  53. private:
  54. CC_SYNTHESIZE(CCNode *,_currentNode,CurrentNode);
  55. const int maxNum;
  56. int roleNum;
  57.  
  58. CCArray * _nodeArr;
  59.  
  60. bool isTouchDown;
  61.  
  62. CCPoint beginPt;
  63.  
  64. void blackSprite(CCNode* sprite);
  65. void recoverFromBalck(CCNode* sprite);
  66.  
  67. std::vector<CCPoint> ptVec;
  68.  
  69. int cgm;//椭圆的倾斜角度
  70. int radius;//半径
  71.  
  72. bool isMoveTimeOut;//一次滑动结束,才能开启下一次滑动
  73. };
  1. </pre><pre name="code" class="cpp"><pre name="code" class="cpp">两个类,<span style="font-family: Arial,Helvetica,sans-serif;">nodeAndPt 绑定每个节点和坐标。</span>
  1. <span style="font-family: Arial,sans-serif;"></span><pre name="code" class="cpp">MyRoleScrollView负责滑动
  1. create函数有三个参数,第一个是数组,每一个都是节点,可以是精灵或者panel,可以包含子节点。
  1. 第二参数和第三参数分别是圆的半径和圆的倾斜度数
  1. </pre><pre name="code" class="cpp">接下来,大家自己看cpp文件吧。
  1. </pre><pre name="code" class="cpp">
  1. </pre><pre name="code" class="cpp">
  1. //MyRoleScrollView.cpp
  2.  
  3. #include "MyRoleScrollView.h"
  4.  
  5. #define SHADE_TAG 1234
  6. #define induration 0.2f
  7. #define _scale 0.6f
  8.  
  9. #define PIE 3.141592653
  10.  
  11.  
  12.  
  13.  
  14. MyRoleScrollView * MyRoleScrollView::create(CCArray * nodeArr,int radius)
  15. {
  16. MyRoleScrollView * node = new MyRoleScrollView;
  17. if(node&&node->init(nodeArr,cgm,radius))
  18. {
  19. node->autorelease();
  20. }
  21. else
  22. delete node;
  23.  
  24. return node;
  25. }
  26. void MyRoleScrollView::addRole(CCNode * node)
  27. {
  28. if(!node)
  29. return;
  30.  
  31. if(_nodeArr->count()<this->maxNum)
  32. {
  33. nodeAndPt * p = new nodeAndPt(node,roleNum);
  34. _nodeArr->addObject(p);
  35. roleNum++;
  36. }
  37.  
  38. refreshPt();
  39. }
  40.  
  41. bool MyRoleScrollView::init(CCArray * nodeArr,int radius)
  42. {
  43. if(!CCNode::init())
  44. return false;
  45. initData(nodeArr,radius);
  46. initView();
  47. return true;
  48. }
  49.  
  50. void MyRoleScrollView::initData(CCArray * nodeArr,int radius)
  51. {
  52. if(!nodeArr||nodeArr->count()<1)
  53. return;
  54.  
  55. this->cgm = cgm;
  56. this->radius = radius;
  57.  
  58. _nodeArr = CCArray::create();
  59. _nodeArr->retain();
  60.  
  61. for(int i = 0;i<nodeArr->count();i++)
  62. {
  63. CCNode * node = dynamic_cast<CCNode*>(nodeArr->objectAtIndex(i));
  64. nodeAndPt * p = new nodeAndPt(node,i);
  65. _nodeArr->addObject(p);
  66. if(i==0)
  67. {
  68. _currentNode = node;
  69. }
  70. }
  71. roleNum = _nodeArr->count();
  72.  
  73. refreshPt();
  74. }
  75.  
  76. void MyRoleScrollView::initView()
  77. {
  78. CCPoint pt = this->getPosition();
  79.  
  80.  
  81. for(int i = 0;i<roleNum;i++)
  82. {
  83. nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i));
  84. CCNode *node = p->node;
  85.  
  86. if(i==0)
  87. {
  88. _currentNode = p->node;
  89. //
  90. this->recoverFromBalck(dynamic_cast<CCSprite*>(node));
  91. p->node->setScale(1.0f);
  92. }
  93. else
  94. {
  95. //
  96. this->blackSprite(dynamic_cast<CCSprite*>(node));
  97. p->node->setScale(_scale);
  98. }
  99. node->setPosition(ptVec[p->ptIndex]);
  100.  
  101. this->addChild(node,i);
  102. }
  103. schedule(schedule_selector(MyRoleScrollView::updateOnce),0.8f);
  104. }
  105.  
  106. void MyRoleScrollView::onEnter()
  107. {
  108. CCNode::onEnter();
  109. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-1,true);
  110. }
  111.  
  112. void MyRoleScrollView::onExit()
  113. {
  114. CCNode::onExit();
  115. CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
  116. }
  117.  
  118. bool MyRoleScrollView::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent)
  119. {
  120. if(roleNum<2)
  121. return false;
  122.  
  123. CCNode * node= _currentNode;
  124.  
  125. CCPoint pt = node->convertToNodeSpace(pTouch->getLocation());
  126.  
  127. CCRect rect(-node->getContentSize().width,node->getContentSize().width*3,node->getContentSize().height);
  128. if(rect.containsPoint(pt))
  129. {
  130. isTouchDown = true;
  131. beginPt = pTouch->getLocation();
  132. }
  133. return true;
  134. }
  135. void MyRoleScrollView::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent)
  136. {
  137. if(!isTouchDown||!isMoveTimeOut)
  138. return;
  139.  
  140. CCPoint pt = pTouch->getLocation();
  141.  
  142. int moveX = pt.x-beginPt.x;
  143. if(moveX>50)//向右滑
  144. {
  145. isTouchDown = false;
  146. for(int i = 0;i<roleNum;i++)
  147. {
  148. nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i));
  149. setPt(p,p->ptIndex+1);
  150.  
  151. }
  152. isMoveTimeOut = false;
  153. scheduleOnce(schedule_selector(MyRoleScrollView::updateOnce),induration);
  154. }
  155. else if(moveX<-50)//向左划
  156. {
  157. isTouchDown = false;
  158. for(int i = 0;i<roleNum;i++)
  159. {
  160. nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i));
  161. setPt(p,p->ptIndex-1);
  162. }
  163. isMoveTimeOut = false;
  164. scheduleOnce(schedule_selector(MyRoleScrollView::updateOnce),induration);
  165. }
  166. }
  167. void MyRoleScrollView::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)
  168. {
  169. isTouchDown = false;
  170. }
  171. void MyRoleScrollView::ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent)
  172. {
  173.  
  174. }
  175.  
  176. void MyRoleScrollView::refreshPt()
  177. {
  178. ptVec.clear();
  179. int angle = 360/roleNum;
  180. for(int i = 0,j = -90.0f;i<roleNum;i++)
  181. {
  182. double hudu = j*PIE/180;
  183. float g_sin = sin(hudu);
  184. float g_cos = cos(hudu);
  185. int x = MyRoleScrollView::radius*g_cos;
  186. int y = MyRoleScrollView::radius*g_sin*cos(cgm*PIE/180);
  187.  
  188. ptVec.push_back(CCPoint(x,y));
  189.  
  190. j+=angle;
  191. }
  192. }
  193.  
  194. void MyRoleScrollView::setPt(nodeAndPt * p,int i)
  195. {
  196. if(i == -1)
  197. i = roleNum-1;
  198. else if(i == roleNum)
  199. i = 0;
  200.  
  201. ccBezierConfig tr0;
  202. float a = p->node->getPosition().x;
  203. float b = p->node->getPosition().y;
  204. float c = ptVec[i].x;
  205. float d = ptVec[i].y;
  206.  
  207. float m = (a+c)/4;
  208. float n = (b+d)/4;
  209.  
  210. CCPoint pt1 = ccpAdd(p->node->getPosition(),ccp(m,n));
  211. CCPoint pt2 = ccpAdd(ptVec[i],n));
  212.  
  213. tr0.endPosition=ptVec[i];
  214. tr0.controlPoint_1=pt1;
  215. tr0.controlPoint_2=pt2;
  216. if(i==0)
  217. {
  218. _currentNode = p->node;
  219. this->recoverFromBalck(dynamic_cast<CCSprite*>(p->node));
  220. }
  221. else
  222. {
  223. this->blackSprite(dynamic_cast<CCSprite*>(p->node));
  224. }
  225. CCActionInterval* bezierForward = CCBezierTo::create(induration,tr0);
  226.  
  227.  
  228. CCSpawn * sp=NULL;
  229. if(i==0)
  230. sp = CCSpawn::create(CCScaleTo::create(induration,1),bezierForward,NULL);
  231. else
  232. sp = CCSpawn::create(CCScaleTo::create(induration,_scale),NULL);
  233.  
  234. p->node->runAction(sp);
  235. p->ptIndex = i;
  236. }
  237.  
  238. void MyRoleScrollView::blackSprite(CCNode* sprite)
  239. {
  240. //unsigned int width = sprite->getTexture()->getPixelsWide();
  241. //unsigned int height = sprite->getTexture()->getPixelsHigh();
  242.  
  243. if(sprite->getChildByTag(SHADE_TAG)!=NULL)
  244. return;
  245.  
  246. CCPoint position = sprite->getPosition();
  247.  
  248. unsigned int width = sprite->getContentSize().width;
  249. unsigned int height = sprite->getContentSize().height;
  250. CCRenderTexture* r = CCRenderTexture::create(width,height);
  251. r->beginWithClear(1,1,0);
  252. sprite->setPosition(ccp(width / 2.0,height/ 2.0)); // Node: set position here!
  253. sprite->visit();
  254. r->end();
  255. // create a new CCImage
  256. CCImage* image = r->newCCImage();
  257. width = image->getWidth();
  258. height= image->getHeight();
  259. // this data is the texture data in memery
  260. unsigned char* data = image->getData();
  261. typedef enum {
  262. RED = 0,GREEN = 1,BLUE = 2,ALPHA = 3
  263. } PIXELS;
  264. // convert unsigned char*(1 Byte) to uint_32_t(4 Bytes)
  265. uint32_t *pixels = (uint32_t *)data;
  266. for(int y = 0; y < height; y++) {
  267. for(int x = 0; x < width; x++) {
  268. uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
  269. uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
  270. //uint32_t gray = 0.3 * rgbaPixel[RED] + 0.3 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
  271. // set the pixels to gray
  272. rgbaPixel[RED] = gray;
  273. rgbaPixel[GREEN] = gray;
  274. rgbaPixel[BLUE] = gray;
  275. }
  276. }
  277. // create a new CCTexture2D based on the CCImage data modified above
  278. CCTexture2D* texture = new CCTexture2D();
  279. texture->initWithImage(image);
  280.  
  281. CCSprite * s = CCSprite::create();
  282. s->initWithTexture(texture);
  283. // release other resources
  284. r->release();
  285. image->release();
  286.  
  287. CCArray * arrChild = sprite->getChildren();
  288. CCObject *obj;
  289. CCARRAY_FOREACH(arrChild,obj)
  290. {
  291. CCNode * node = dynamic_cast<CCNode*>(obj);
  292. node->setVisible(false);
  293. }
  294.  
  295. s->setAnchorPoint(ccp(0,0));
  296. s->setPosition(ccp(0,0));
  297. s->setOpacity(200);
  298. s->setTag(SHADE_TAG);
  299. sprite->addChild(s);
  300.  
  301. sprite->setPosition(position);
  302. }
  303.  
  304. void MyRoleScrollView::recoverFromBalck(CCNode* sprite)
  305. {
  306. if(sprite->getChildByTag(SHADE_TAG)==NULL)
  307. return;
  308.  
  309. sprite->removeChildByTag(SHADE_TAG);
  310.  
  311. CCArray * arrChild = sprite->getChildren();
  312. CCObject *obj;
  313. CCARRAY_FOREACH(arrChild,obj)
  314. {
  315. CCNode * node = dynamic_cast<CCNode*>(obj);
  316. node->setVisible(true);
  317. }
  318. }
  319.  
  320. void MyRoleScrollView::updateOnce(float t)
  321. {
  322. isMoveTimeOut = true;
  323. for(int i = 0;i<roleNum;i++)
  324. {
  325. nodeAndPt * p = dynamic_cast<nodeAndPt*>(_nodeArr->objectAtIndex(i));
  326. setPt(p,p->ptIndex+1);
  327.  
  328. }
  329. isMoveTimeOut = false;
  330. }

猜你在找的Cocos2d-x相关文章