app.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /**
  2. * Created by quanchen on 2014/8/26.
  3. */
  4. (function(arg)
  5. {
  6. (arg.init=function(){
  7. //this.initialize();
  8. console.log('app run');
  9. //var loading=new loadingLib.LOADING();
  10. //stage.addChild(loading);
  11. var mc=new createjs.MovieClip();
  12. var story=new lib.STORY();//情节1;
  13. var story1;//情节2;
  14. var main;//游戏主画面;
  15. var end;//
  16. var hmc;//横屏;
  17. var numArrClass=[lib.NUM0,lib.NUM1,lib.NUM2,lib.NUM3,lib.NUM4,lib.NUM5,lib.NUM6,lib.NUM7,lib.NUM8,lib.NUM9];
  18. //游戏设定;
  19. //1 2 3 随机;
  20. var TIMESARRCON=[1,1,2,2,3,3,3,4,4,5,5];
  21. var timesArr=[1,1,2,2,3,3,3,4,4];
  22. var moonPlayTimesArr=[];//记录月饼飞行时间;
  23. var score=0;
  24. var starNum=5;
  25. var mainTime;//延迟数据记录;
  26. var shootTime=550;//
  27. //end;
  28. var isShare=false;
  29. stage.addChild(mc);
  30. //test;
  31. //gotoMain();
  32. //gotoEnd();
  33. gotoStroy();
  34. var defultW=640;
  35. var defultH=1008;
  36. window.onresize=resizeCanvas;
  37. resizeCanvas();
  38. hmc=new lib.HMC();
  39. function resizeCanvas()
  40. {
  41. var pro = defultW / defultH;
  42. var w = window.innerWidth;
  43. var h = window.innerHeight;
  44. if (w > h)//横屏
  45. {
  46. //root.h_mc.visible=true;
  47. //hmc=new lib.HMC();
  48. mc.addChild(hmc);
  49. defultW = 756;
  50. defultH = 480;
  51. if (pro < (w / h)) {
  52. scale = mc.scaleX = mc.scaleY = w / defultW;
  53. }
  54. else {
  55. scale = mc.scaleX = mc.scaleY = h / defultH;
  56. }
  57. }
  58. else//竖屏
  59. {
  60. //root.h_mc.visible=false;
  61. //if(hmc)
  62. //{
  63. mc.removeChild(hmc);
  64. //}
  65. defultW = 480;
  66. defultH = 756;
  67. if (pro > (w / h)) {
  68. scale = mc.scaleX = mc.scaleY = w / defultW;
  69. }
  70. else {
  71. scale = mc.scaleX = mc.scaleY = h / defultH;
  72. }
  73. }
  74. wrongx = mc.x = (w - (scale * defultW)) / 2;
  75. mc.y = (h - (scale * defultH)) / 2;
  76. canvas.width = w;
  77. canvas.height = h;
  78. }
  79. function gotoStroy()
  80. {
  81. mc.addChild(story);
  82. canvas.addEventListener('touchstart',onTouchStart,true);
  83. story._mc.mc0.name='mc0';
  84. }
  85. function onTouchMove(e)
  86. {
  87. var touch=e.touches[0];
  88. //console.log('touch.pageY:'+touch.pageY);
  89. var pro=1-(touch.pageY/scale-260)/380;
  90. pro=Math.ceil(pro*14);
  91. if(pro>=14) pro=14;
  92. story._mc.mc1.gotoAndStop(pro);
  93. //console.log('pro:'+pro);
  94. }
  95. function onTouchEnd(e)
  96. {
  97. var touch=e.touches[0];
  98. //console.log(story._mc.mc1)
  99. canvas.removeEventListener('touchmove',onTouchMove,true);
  100. canvas.removeEventListener('touchend',onTouchEnd,true);
  101. if(story._mc.mc1.currentFrame==14)
  102. {
  103. story._mc.mc1.gotoAndPlay(14);
  104. mcFrame(story._mc.mc1,33,gotoStory1)
  105. }
  106. else
  107. {
  108. story._mc.gotoAndStop(0);
  109. if(story._mc.tips_mc) story._mc.removeChild(story._mc.tips_mc);
  110. canvas.addEventListener('touchstart',onTouchStart,true);
  111. }
  112. }
  113. function onTouchStart(e)
  114. {
  115. e.preventDefault();
  116. var touch=e.touches[0];
  117. var obj=checkClickArr(touch,[story._mc.mc0]);
  118. if(obj.click==true)
  119. {
  120. if(obj.mcname=='mc0')
  121. {
  122. canvas.addEventListener('touchmove',onTouchMove,true);
  123. canvas.addEventListener('touchend',onTouchEnd,true);
  124. canvas.removeEventListener("touchstart", onTouchStart,true);
  125. console.log('mouse down mc0');
  126. story._mc.gotoAndStop(1);
  127. }
  128. }
  129. }
  130. //故事1切换到故事2;
  131. function gotoStory1()
  132. {
  133. console.log('gotoStory1');
  134. mc.removeChild(story);
  135. story1=new lib.STORY1();
  136. mc.addChild(story1);
  137. //85 95 99 112;
  138. mcFrame(story1,85,story1NextFun);
  139. mcFrame(story1,95,story1NextFun);
  140. mcFrame(story1,99,story1NextFun);
  141. mcFrame(story1,112,story1NextFun);
  142. }
  143. function story1NextFun()
  144. {
  145. story1.next_mc.name='next_mc';
  146. canvas.addEventListener('touchstart',onStory1TouchStart,true);
  147. }
  148. function onStory1TouchStart(e)
  149. {
  150. e.preventDefault();
  151. var touch=e.touches[0];
  152. var obj=checkClickArr(touch,[story1.next_mc]);
  153. if(obj.click==true)
  154. {
  155. if(obj.mcname=='next_mc')
  156. {
  157. canvas.removeEventListener("touchstart", onStory1TouchStart,true);
  158. if(story1.currentFrame==112)
  159. {
  160. gotoMain();
  161. }
  162. else
  163. {
  164. story1.play();
  165. }
  166. }
  167. }
  168. }
  169. //游戏主画面;
  170. function gotoMain()
  171. {
  172. if(story1) mc.removeChild(story1);
  173. if(end) mc.removeChild(end);
  174. main=new lib.MAIN();
  175. mc.addChild(main);
  176. main.hand_mc.visible=true;
  177. setTimeout(function(){
  178. main.hand_mc.visible=false;
  179. mainShowCannon();
  180. },2000);
  181. canvas.addEventListener('touchstart',onMainTouchStart,true);
  182. /*
  183. setInterval(function(){
  184. mainShowCannon();
  185. },4000);
  186. */
  187. timesArr=TIMESARRCON.slice(0);
  188. starNum=5;
  189. score=0;
  190. //main._txt.text=score.toString();
  191. setNum(main.num_mc,score,1);
  192. main.num_mc.x=530-score.toString().length*50;
  193. var star;
  194. for(var i=0;i<starNum;i++)
  195. {
  196. star=new lib.STAR();
  197. star.y=i*40;
  198. main.star_mc.addChild(star);
  199. }
  200. SHARE_STR='你收到一份来自月球的思念和祝福!快快点击体验吧!';
  201. }
  202. function mainRemoveStar()
  203. {
  204. starNum-=1;
  205. main.star_mc.removeChildAt(0);
  206. if(starNum==0)
  207. {
  208. mainRemove();
  209. setTimeout(gotoEnd,500);
  210. }
  211. console.log('mainRemoveStars');
  212. }
  213. function onMainTouchStart(e)
  214. {
  215. e.preventDefault();
  216. var touch=e.touches[0];
  217. mainHitMoon();
  218. //console.log('touch');
  219. }
  220. function mainHitMoon()
  221. {
  222. if(main.moon_mc.currentFrame>=10&&main.moon_mc.currentFrame<=13)
  223. {
  224. score+=1;
  225. //main._txt.text=score.toString();
  226. setNum(main.num_mc,score,1);
  227. main.num_mc.x=530-score.toString().length*50;
  228. main.rabbits_mc.gotoAndStop(1);
  229. playSound2();
  230. main.moon_mc.gotoAndStop(0);
  231. clickBtnPgv('act.html5MidAutumn.hit');
  232. }
  233. else
  234. {
  235. if(main.moon_mc.currentFrame!=0)
  236. {
  237. removeMcFrame(main.moon_mc,13);
  238. mainRemoveStar();
  239. }
  240. main.rabbits_mc.gotoAndStop(2);
  241. clickBtnPgv('act.html5MidAutumn.miss');
  242. }
  243. setTimeout(function(){
  244. main.rabbits_mc.gotoAndStop(0);
  245. },200);
  246. }
  247. //开始展示火炮;
  248. function mainShowCannon()
  249. {
  250. var times=0;
  251. if(timesArr.length!=0)//循序渐进;
  252. {
  253. times=timesArr[0];
  254. timesArr.splice(0,1);
  255. moonPlayTimesArr=[];
  256. var tmp;
  257. for(var i=0;i<times;i++)
  258. {
  259. tmp=1+Math.floor(Math.random()*2);
  260. moonPlayTimesArr.push(tmp*shootTime);
  261. }
  262. mainPlayCannon(moonPlayTimesArr.slice(0));
  263. if(timesArr.length==0)timesArr=TIMESARRCON.slice(0);
  264. }
  265. else//随机;
  266. {
  267. }
  268. }
  269. //播放炮弹;
  270. function mainPlayCannon(arr)
  271. {
  272. var tmp=0;
  273. var l=arr.length;
  274. var num=arr.length;
  275. tmp-=arr[0];
  276. for(var i=0;i<l;i++)
  277. {
  278. tmp+=arr[0];
  279. arr.splice(0,1);
  280. mainTime=setTimeout(function(){
  281. playSound0();
  282. main.cannon_mc._mc.mc0.gotoAndPlay(1);
  283. num-=1
  284. if(num==0)//
  285. {
  286. //console.log(moonPlayTimesArr.slice(0));
  287. mainPlayMoon(moonPlayTimesArr.slice(0));
  288. }
  289. },tmp);
  290. }
  291. }
  292. //播放月饼;
  293. function mainPlayMoon(arr)
  294. {
  295. mainTime=setTimeout(function(){
  296. var tmp=0;
  297. var l=arr.length;
  298. var num=arr.length;
  299. for(var i=0;i<l;i++)
  300. {
  301. tmp+=arr[0];
  302. arr.splice(0,1);
  303. mainTime=setTimeout(function(){
  304. playSound1();
  305. main.moon_mc.gotoAndPlay(1);
  306. mcFrame(main.moon_mc,13,function(){
  307. mainRemoveStar();
  308. })
  309. num-=1;
  310. if(num==0)//开启下一个大炮;
  311. {
  312. mainTime=setTimeout(mainShowCannon,1000);
  313. }
  314. },tmp);
  315. }
  316. },500);
  317. }
  318. function mainRemove()
  319. {
  320. clearTimeout(mainTime);
  321. canvas.removeEventListener('touchstart',onMainTouchStart,true);
  322. }
  323. //
  324. function gotoEnd()
  325. {
  326. SHARE_STR='你插中了'+score.toString()+'枚从月球飞来的月饼,快快与你的家人一起享用吧!';
  327. mc.removeChild(main);
  328. end=new lib.END();
  329. mc.addChild(end);
  330. mcFrame(end,59,endEndFrame);
  331. }
  332. function endRemove()
  333. {
  334. canvas.removeEventListener('touchstart',onEndTouchStart,true);
  335. }
  336. function endEndFrame()
  337. {
  338. //end._txt0.text=score.toString();
  339. //score=55;
  340. setNum(end.num_mc,score,1);
  341. setNum(end.num0_mc,score,0.5);
  342. end.num_mc.x=25+(640-score.toString().length*50)/2;
  343. end.num0_mc.x=25*0.5+(430-score.toString().length*50*0.5)/2;
  344. //end._txt1.text=score.toString();
  345. end.restart_mc.name='restart_mc';
  346. end.share_mc.name='share_mc';
  347. canvas.addEventListener('touchstart',onEndTouchStart,true);
  348. }
  349. function onEndTouchStart(e)
  350. {
  351. e.preventDefault();
  352. var touch=e.touches[0];
  353. var obj=checkClickArr(touch,[end.restart_mc,end.share_mc]);
  354. if(obj.click==true&&!isShare)
  355. {
  356. if(obj.mcname=='restart_mc')
  357. {
  358. endRestartFun();
  359. }
  360. else if(obj.mcname=='share_mc')
  361. {
  362. endShareFun();
  363. }
  364. }
  365. else if(isShare)
  366. {
  367. isShare=false;
  368. //end.bg_mc.visible=false;
  369. //end.tips_mc.visible=false;
  370. console.log('end.children:'+end.children.length)
  371. end.removeChildAt(end.children.length-1);
  372. end.removeChildAt(end.children.length-1);
  373. }
  374. }
  375. function endRestartFun()
  376. {
  377. gotoMain();
  378. endRemove();
  379. clickBtnPgv('act.html5MidAutumn.restart');
  380. }
  381. function endShareFun()
  382. {
  383. isShare=true;
  384. var bg=new lib.SHAREBG();
  385. var tips=new lib.SHARETIPS();
  386. end.addChild(bg);
  387. end.addChild(tips);
  388. tips.x=480-60+(((window.innerWidth-(480*scale))/2)/scale);
  389. clickBtnPgv('act.html5MidAutumn.share');
  390. }
  391. //指定MC 指定帧 添加函数;
  392. function mcFrame(mc,frame,fun)
  393. {
  394. clearInterval(mc['num'+frame.toString()]);
  395. var num=setInterval(function(){
  396. if(mc.currentFrame==frame)
  397. {
  398. clearInterval(mc['num'+frame.toString()]);
  399. fun();
  400. }
  401. },30);
  402. mc['num'+frame.toString()]=num;
  403. }
  404. function removeMcFrame(mc,frame)
  405. {
  406. clearInterval(mc['num'+frame.toString()]);
  407. }
  408. function setNum(arg,argNum,argScale)
  409. {
  410. var l=arg.children.length;
  411. for(var i= 0;i<l;i++)
  412. {
  413. arg.removeChildAt(0);
  414. }
  415. var str=argNum.toString();
  416. var num;
  417. for(var i=0;i<str.length;i++)
  418. {
  419. num=new numArrClass[Number(str.substr(i,1))]();
  420. num.scaleX=num.scaleY=argScale;
  421. arg.addChild(num);
  422. num.x=argScale*55*i;
  423. }
  424. }
  425. })
  426. })(app=app||{});
  427. var app;
  428. /* |xGv00|6242571a97844cc978495a430881fa4c */