Cocos2d-X中的动作展示《二》

前端之家收集整理的这篇文章主要介绍了Cocos2d-X中的动作展示《二》前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
由于Cocos2d-X中的动作较多,我将所有的动作制作成了一个滚动视图,每个滚动视图上都有动作名,单击滚动视图就可以展示相应的动作

程序效果图:

使用滚动视图实现动作切换


动作展示


首先创建一个ActionMore类

ActionMore.h中的代码

  1. #ifndef _ActionMore_H_
  2. #define _ActionMore_H_
  3.  
  4. #include "cocos2d.h"
  5. #include "cocos-ext.h"
  6. USING_NS_CC;
  7. USING_NS_CC_EXT;
  8.  
  9. class ActionMore : public CCLayer
  10. {
  11. public:
  12. static CCScene* scene();
  13.  
  14. bool init();
  15.  
  16. CREATE_FUNC(ActionMore);
  17. bool ccTouchBegan(CCTouch*,CCEvent*);
  18. void ccTouchEnded(CCTouch*,CCEvent*);
  19.  
  20. CCNode* _c;
  21. void testAction(int idx,CCLayerColor*);
  22. };
  23.  
  24. #endif

ActionMore.cpp中的代码

  1. #include "ActionMore.h"
  2.  
  3. static const char* _actionName[] =
  4. {
  5. "CCEaseBounceIn","CCEaseBounceOut","CCEaseBounceInOut","CCEaseBackIn","CCEaseBackOut","CCEaseBackInOut","CCEaseElasticIn","CCEaseElasticOut","CCEaseElasticInOut","CCEaseExponentialIn","CCEaseExponentialOut","CCEaseExponentialInOut","CCEaseIn","CCEaSEOut","CCEaseInOut","CCEaseSineIn","CCEaseSineOut","CCEaseSineInOut","CCSpeed","CCSpawn","CCSequence","CCRepeat","CCRepeatForever"
  6. };
  7.  
  8. CCScene* ActionMore::scene()
  9. {
  10. CCScene* scene = CCScene::create();
  11.  
  12. ActionMore* layer = ActionMore::create();
  13.  
  14. scene->addChild(layer);
  15.  
  16. return scene;
  17. }
  18.  
  19.  
  20. bool ActionMore::init()
  21. {
  22. CCLayer::init();
  23. CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  24.  
  25. //创建Node结点用于ScrollView
  26. CCNode* c = CCNode::create();
  27. _c = c;
  28.  
  29. //ScrollView中展示的动作的个数
  30. int actionCount = sizeof(_actionName) / sizeof(*_actionName);
  31. //使用for循环创建视图用于展示动作
  32. for (int i = 0; i < actionCount; i++)
  33. {
  34. CCLayerColor* layer;
  35. //
  36. if (i % 2 == 0)
  37. {
  38. //创建带有颜色的背景层(背景层的颜色为深灰色)
  39. layer = CCLayerColor::create(ccc4(192,192,255),winSize.width,winSize.height);
  40. }
  41. else
  42. {
  43. //创建带有颜色的背景层(背景层的颜色为浅灰色)
  44. layer = CCLayerColor::create(ccc4(128,128,winSize.height);
  45. }
  46.  
  47. c->addChild(layer);
  48. layer->setPosition(ccp(i*winSize.width,0));
  49.  
  50. //保存动作的名字
  51. const char* title = _actionName[i];
  52.  
  53. //创建标签用于显示动作的名字
  54. CCLabelTTF* label = CCLabelTTF::create(title,"Arial",36);
  55. layer->addChild(label);
  56.  
  57. //设置标签的位置
  58. label->setPosition(ccp(winSize.width / 2,winSize.height - 80));
  59. }
  60.  
  61. //创建滚动视图
  62. CCScrollView* view = CCScrollView::create(winSize,c);
  63. //设置滚动视图的滚动方向为水平滚动
  64. view->setDirection(kCCScrollViewDirectionHorizontal);
  65.  
  66. //设置滚动视图的大小
  67. view->setContentSize(CCSize(winSize.width*actionCount,winSize.height));
  68. addChild(view);
  69.  
  70. //能触摸
  71. setTouchEnabled(true);
  72. setTouchMode(kCCTouchesOneByOne);
  73.  
  74. return true;
  75. }
  76.  
  77. bool ActionMore::ccTouchBegan(CCTouch*,CCEvent*)
  78. {
  79. return true;
  80. }
  81.  
  82. void ActionMore::testAction(int idx,CCLayerColor* layer)
  83. {
  84. CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  85. //得到用户创建的精灵
  86. CCSprite* sprite = (CCSprite*)layer->getUserObject();
  87.  
  88. //当没有精灵的时候
  89. if (sprite == NULL)
  90. {
  91. //创建一个新的精灵
  92. sprite = CCSprite::create("CloseNormal.png");
  93. layer->addChild(sprite);
  94.  
  95. //设置精灵的关联对象
  96. layer->setUserObject(sprite);
  97. }
  98.  
  99. //保存用户选择的动作
  100. const char* an = _actionName[idx];
  101. //动作类
  102. CCAction* action;
  103. //设置精灵的位置
  104. sprite->setPosition(ccp(winSize.width / 2,winSize.height / 2));
  105.  
  106. CCMoveBy* moveBy = CCMoveBy::create(4,ccp(0,sprite->getContentSize().height / 2 - winSize.height / 2));
  107.  
  108. action= NULL;
  109.  
  110. if (an == "CCEaseBounceIn")//让目标动作具有反弹效果,从起点反弹
  111. {
  112. action = CCEaseBounceIn::create(moveBy);
  113. }
  114. if (an == "CCEaseBounceOut")//让目标动作具有反弹效果,从终点反弹
  115. {
  116. action = CCEaseBounceOut::create(moveBy);
  117. }
  118. if (an == "CCEaseBounceInOut")//让目标动作具有反弹效果,起点终点都反弹
  119. {
  120. action = CCEaseBounceInOut::create(moveBy);
  121. }
  122. if (an == "CCEaseBackIn")//让目标动作具有回力效果,起点作为回力点
  123. {
  124. action = CCEaseBackIn::create(moveBy);
  125. }
  126. if (an == "CCEaseBackOut")//让目标动作具有回力效果,终点作为回力点
  127. {
  128. action = CCEaseBackOut::create(moveBy);
  129. }
  130. if (an == "CCEaseBackInOut")//让目标动作具有回力效果,起点终点都作为回力点
  131. {
  132. }
  133. if (an == "CCEaseElasticIn")//让目标动作具有弹性效果,起点具有弹性
  134. {
  135. action = CCEaseElasticIn::create(moveBy);
  136. }
  137. if (an == "CCEaseElasticOut")//让目标动作具有弹性效果,终点具有弹性
  138. {
  139. action = CCEaseElasticOut::create(moveBy);
  140. }
  141. if (an == "CCEaseElasticInOut")//让目标动作具有弹力效果,起点终点都具有弹性
  142. {
  143. action = CCEaseElasticInOut::create(moveBy);
  144. }
  145. if (an == "CCEaseExponentialIn")//让目标动作缓慢开始
  146. {
  147. action = CCEaseExponentialIn::create(moveBy);
  148. }
  149. if (an == "CCEaseExponentialOut")//让目标动作缓慢结束
  150. {
  151. action = CCEaseExponentialOut::create(moveBy);
  152. }
  153. if (an == "CCEaseExponentialInOut")//让目标动作缓慢开始并缓慢结束
  154. {
  155. action = CCEaseExponentialInOut::create(moveBy);
  156. }
  157. if (an == "CCEaseIn")//让目标动作由慢到快(速度线性变化)
  158. {
  159. action = CCEaseIn::create(moveBy,10.0f);
  160. }
  161. if (an == "CCEaSEOut")//让目标动作由快到慢(速度线性变化)
  162. {
  163. action = CCEaSEOut::create(moveBy,10.f);
  164. }
  165. if (an == "CCEaseInOut")//让目标动作由慢到快再到慢(速度线性变化)
  166. {
  167. action = CCEaseInOut::create(moveBy,10.f);
  168. }
  169. if (an == "CCEaseSineIn")//让目标动作由慢到快(速度非线性变化)
  170. {
  171. action = CCEaseSineIn::create(moveBy);
  172. }
  173. if (an == "CCEaseSineOut")//让目标动作由快到慢(速度非线性变化)
  174. {
  175. action = CCEaseSineOut::create(moveBy);
  176. }
  177. if (an == "CCEaseSineInOut")//让目标动作由慢到快再到慢(速度非线性变化)
  178. {
  179. action = CCEaseSineInOut::create(moveBy);
  180. }
  181. if (an == "CCSpeed")//为目标动作速度翻倍(加速)
  182. {
  183. action = CCSpeed::create(moveBy,10);
  184. }
  185.  
  186. if (action)
  187. {
  188. sprite->runAction(action);
  189. }
  190.  
  191. }
  192.  
  193. void ActionMore::ccTouchEnded(CCTouch* t,CCEvent*)
  194. {
  195. //得到按下鼠标时的位置
  196. CCPoint ptStart = t->getStartLocation();
  197. //得到松开鼠标时的位置
  198. CCPoint ptEnd = t->getLocation();
  199. //如果两个位置的距离的平方小于或者等于25
  200. if(ptStart.getDistanceSq(ptEnd) <= 25)
  201. {
  202. // click
  203. // 点中了哪个子窗口
  204. // 转换ptStart为ScrollView中的Container的坐标
  205. // 再判断被点击的LayerColor
  206. //将鼠标点下的时候的位置的坐标转换成结点坐标
  207. CCPoint ptInContainer = _c->convertToNodeSpace(ptStart);
  208. //创建一个数组用于保存LayerColor
  209. CCArray* arr = _c->getChildren();// 所有的layercolor
  210. //用于寻找点中的LayerColor
  211. for (int i = 0; i < sizeof(_actionName) / sizeof(*_actionName); i++)
  212. {
  213. //
  214. CCLayerColor* layer = (CCLayerColor*)arr->objectAtIndex(i);
  215. if (layer->boundingBox().containsPoint(ptInContainer))
  216. {
  217. testAction(i,layer);
  218. break;
  219. }
  220. }
  221. }
  222.  
  223. }
  1.  


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