Cocos2d-X开发中提供了非常丰富的动作特效
如:网格动画
扭曲特效
3D瓷砖波动特效
代码示例:
1. <fontface="Tahoma">#include "ActionEffect.h"
2. #include"HelloWorldScene.h"
3.
4. staticconst char* _actionName[] =
5. {
6. "CCFadeOutBLTiles",
7. "CCFadeOutDownTiles",
8. "CCFadeOutTRTiles",
9. "CCFadeOutUpTiles",
10.
11. "CCFlipX3D",
12. "CCFlipY3D",
13.
14. "CCJumpTiles3D",
15.
16. "CCLens3D",
17.
18. "CCLiquid",
19.
20. "CCPageTurn3D",
21.
22. "CCRipple3D",
23.
24. "CCShaky3D",
25.
26. "CCShakyTiles3D",
27.
28. "CCShatteredTiles3D",
29.
30. "CCShuffleTiles",
31.
32. "CCSplitCols",
33. "CCSplitRows",
34. "CCTurnOffTiles",
35. "CCTwirl",
36. "CCWaves",
37. "CCWaves3D",
38. "CCWavesTiles3D"
39.
40. };
41.
42. CCScene*ActionEffect::scene()
43. {
44. CCScene* s = CCScene::create();
45. ActionEffect* layer = ActionEffect::create();
46. s->addChild(layer);
47. return s;
48. }
49.
50. boolActionEffect::init()
51. {
52. CCLayer::init();
53.
54. CCSize winSize =CCDirector::sharedDirector()->getWinSize();
55.
56. CCNode* c = CCNode::create();
57. _c = c;
58. int actionCount = sizeof(_actionName) /sizeof(*_actionName);
59.
60. for (int i = 0; i < actionCount; i++)
61. {
62. /*
63. CCSprite* bg =CCSprite::create("HelloWorld.png");
64. c->addChild(bg);
65. bg->setPosition(ccp(winSize.width / 2 +i*winSize.width, winSize.height / 2));
66. */
67. CCLayerColor* layer;
68. if (i % 2 == 0)
69. {
70. layer =CCLayerColor::create(ccc4(192, 192, 192, 255), winSize.width,winSize.height);
71. }
72. else
73. {
74. layer =CCLayerColor::create(ccc4(128, 128, 128, 255), winSize.width,winSize.height);
75. }
76. c->addChild(layer);
77. layer->setPosition(ccp(i*winSize.width,0));
78.
79. /* 设置Title */
80. const char* title = _actionName[i];
81. CCLabelTTF* label = CCLabelTTF::create(title,"Arial", 36);
82. layer->addChild(label, 1000);
83. label->setPosition(ccp(winSize.width / 2,winSize.height - 80));
84. }
85.
86. CCScrollView* view = CCScrollView::create(winSize, c);
87. view->setDirection(kCCScrollViewDirectionHorizontal);
88. view->setContentSize(CCSize(winSize.width*actionCount,winSize.height));
89. addChild(view);
90.
91. c->setPositionX((1 - actionCount)*winSize.width);
92.
93. // 能触摸
94. setTouchEnabled(true);
95. setTouchMode(kCCTouchesOneByOne);
96.
97. return true;
98. }
99.
100.boolActionEffect::ccTouchBegan(CCTouch*, CCEvent*)
101.{
102. return true;
103.}
104.
105.voidActionEffect::testAction(int idx, CCLayerColor* layer)
106.{
107. CCSize winSize =CCDirector::sharedDirector()->getWinSize();
108. CCSprite* sprite = (CCSprite*)layer->getUserObject();
109.
110. if (sprite == NULL)
111. {
112. // sprite =CCSprite::create("background3.png");
113. sprite =CCSprite::create("HelloWorld.png");
114. layer->setUserObject(sprite);
115. layer->addChild(sprite);
116. }
117. const char* an = _actionName[idx];
118. CCAction* action = NULL;
119. sprite->setPosition(ccp(winSize.width / 2, winSize.height /2));
120. CCMoveBy* moveBy = CCMoveBy::create(4, ccp(0,sprite->getContentSize().height / 2 - winSize.height / 2));
121.
122. //网格从右上到左下部消失
123. //第一个参数:时间
124. //第二个参数:网格大小
125. if (an == "CCFadeOutBLTiles")
126. {
127. action = CCFadeOutBLTiles::create(5, CCSize(16,12));
128. }
129.
130. //网格从上到下折叠消失
131. //第一个参数:时间
132. //第二个参数:网格大小
133. if (an == "CCFadeOutDownTiles")
134. {
135. action = CCFadeOutDownTiles::create(5,CCSize(16, 12));
136. }
137.
138. //网格从左下到右上消失
139. if (an == "CCFadeOutTRTiles")
140. {
141. action = CCFadeOutTRTiles::create(5, CCSize(16,12));
142. }
143.
144. //网格从下到上消失
145. if (an == "CCFadeOutUpTiles")
146. {
147. action = CCFadeOutUpTiles::create(5, CCSize(16,12));
148. }
149.
150. //创建一个X轴3D反转特效
151. if (an == "CCFlipX3D") // 影响touch
152. {
153. action = CCFlipX3D::create(5);
154. }
155.
156. //创建一个Y轴3D反转特效
157. if (an == "CCFlipY3D")
158. {
159. action = CCFlipY3D::create(5);;
160. }
161.
162. //网格跳动特效
163. if (an == "CCJumpTiles3D")
164. { //参数:时间,网格大小,次数,振幅
165. action = CCJumpTiles3D::create(5, CCSize(16,12), 56, 5.0f);
166. }
167.
168. //凸透镜特效
169. if (an == "CCLens3D")
170. {
171. //参数:时间,网格大小,圆心坐标,圆半径
172. action = CCLens3D::create(5, CCSize(16, 12),ccp(winSize.width / 2, winSize.height / 2), 100);
173. }
174.
175. //液体特效
176. if (an == "CCLiquid")
177. {
178. //参数:时间,网格大小,速度,振幅
179. action = CCLiquid::create(56, CCSize(16, 12),56, 2.0f);
180. }
181.
182. //3D翻页特效
183. if (an == "CCPageTurn3D")
184. {
185. action = CCPageTurn3D::create(5, CCSize(16,12));
186. }
187.
188. //创建一个3D水波特效
189. if (an == "CCRipple3D")
190. {
191. //参数:时间,网格大小,坐标,半径,速度,振幅
192. action = CCRipple3D::create(10, CCSize(16, 12),ccp(winSize.width / 2, winSize.height / 2), 100, 10, 50.0f);
193. }
194.
195. //创建一个3D晃动的效果
196. if (an == "CCShaky3D")
197. {
198. //参数:时间,晃动网格大小,晃动范围,Z轴是否晃动
199. action = CCShaky3D::create(10, CCSize(16, 12),2, true);
200. }
201.
202. //创建一个3D瓷砖晃动的效果
203. if (an == "CCShakyTiles3D")
204. {
205. //参数:时间,晃动网格大小,晃动范围,Z轴是否晃动
206. action = CCShakyTiles3D::create(5, CCSize(16,12), 2, true);
207. }
208.
209. //创建一个3D破碎瓷砖特效
210. if (an == "CCShatteredTiles3D")
211. {
212. //参数:时间,晃动网格大小,晃动范围,Z轴是否晃动
213. action = CCShatteredTiles3D::create(1,CCSize(16, 12), 7, true);
214. }
215.
216. //瓷砖洗牌特效
217. if (an == "CCShuffleTiles")
218. {
219. //参数:时间,网格大小,随机数
220. action = CCShuffleTiles::create(5, CCSize(16,12), CCRANDOM_0_1()*10000);
221. }
222.
223. //多行消失特效(垂直)
224. if (an == "CCSplitCols")
225. {
226. //参数:时间,行数
227. action = CCSplitCols::create(5, 9);
228. }
229.
230. //多行消失特效(水平)
231. if (an == "CCSplitRows")
232. {
233. //参数:时间,行数
234. action = CCSplitRows::create(5, 9);
235. }
236.
237. //方块消失
238. if (an == "CCTurnOffTiles")
239. {
240. //参数:时间,网格大小,随机数
241. action = CCTurnOffTiles::create(5, CCSize(16,12));
242. }
243.
244. //创建一个扭曲特效
245. if (an == "CCTwirl")
246. {
247. //时间,网格大小,坐标,扭曲次数,振幅
248. action = CCTwirl::create(5, CCSize(16, 12),ccp(winSize.width / 2, winSize.height / 2), 5, 5.0f);
249. }
250.
251. //创建一个波动效果
252. if (an == "CCWaves")
253. {
254. //参数:时间,晃动网格大小,波动速度,振幅,是否水平波动,是否垂直波动
255. action = CCWaves::create(5, CCSize(16, 12), 5,5.0f, true, false);
256. }
257.
258. //创建一个3D波动效果
259. if (an == "CCWaves3D")
260. {
261. //参数:时间,晃动网格大小,波动速度,振幅
262. action = CCWaves3D::create(5, CCSize(16, 12), 5,5.0f);
263. }
264.
265. //创建一个3D瓷砖波动效果
266. if (an == "CCWavesTiles3D")
267. {
268. //参数:时间,晃动网格大小,波动速度,振幅
269. action = CCWavesTiles3D::create(5, CCSize(16,12), 5, 5.0f);
270. }
271.
272. if (action)
273. {
274. sprite->runAction(action);
275. }
276.
277.}
278.
279.voidActionEffect::ccTouchEnded(CCTouch* t, CCEvent*)
280.{
281. CCPoint ptStart = t->getStartLocation();
282. CCPoint ptEnd = t->getLocation();
283. if (ptStart.getDistanceSq(ptEnd) <= 25)
284. {
285. // click
286. // 点中了哪个子窗口
287.
288. // 转换ptStart为ScrollView中的Container的坐标
289. // 再判断被点击的LayerColor
290. CCPoint ptInContainer =_c->convertToNodeSpace(ptStart);
291. CCArray* arr = _c->getChildren(); // 所有的layercolor
292. for (int i = 0; i < sizeof(_actionName) /sizeof(*_actionName); i++)
293. {
294. CCLayerColor* layer = (CCLayerColor*)arr->objectAtIndex(i);
295. if(layer->boundingBox().containsPoint(ptInContainer))
296. {
297. testAction(i,layer);
298. break;
299. }
300. }
301. }
302.
303.}</font>
复制代码
更多cocos2d-x学习视频教程,请至9秒学院:http://www.9miaoxueyuan.com/查看。