app.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. var ICONS = [
  2. ['ditu.png', '地图'],
  3. ['huluwa.png', '葫芦兄弟'],
  4. ['beiwanglu.png', '备忘录'],
  5. ['zhuyishixiang.png', '提醒事项'],
  6. ['taobao.png', '手机淘宝'],
  7. ['itunes.png', 'iTunes Store'],
  8. ['weixin.png', '微信'],
  9. ['yinyue.png', '音乐'],
  10. ['qq.png', 'QQ'],
  11. ['baokanzazhi.png', '报刊杂志'],
  12. ['shanku.png', '闪酷'],
  13. ['appstore.png', 'App Store'],
  14. ['tianqi.png', '天气'],
  15. ['shezhi.png', '设置'],
  16. ['youjian.png', '邮件'],
  17. ['2048.png', '2048']
  18. ];
  19. var StartLayer = cc.Layer.extend({
  20. ctor: function(context){
  21. this._super();
  22. this.setBackground('rgba(0, 0, 0, 128)');
  23. var title = cc.createSprite('title.png', {
  24. xy: [320, 700],
  25. });
  26. this.addChild(title);
  27. var buttonSp = cc.Button.create('button_bg.png', {
  28. xy: [320, 380],
  29. scale: 0.88
  30. });
  31. this.addChild(buttonSp);
  32. var btnTxt = cc.createSprite('@开始戳', {
  33. xy: [220, 40],
  34. fontSize: 46,
  35. color: '#00a538'
  36. });
  37. buttonSp.addChild(btnTxt);
  38. buttonSp.scaleTo(0.5, 0.92).scaleTo(0.5, 0.85).delay(1.0).repeat().act();
  39. var self = this;
  40. this.delegate(buttonSp, 'click', function(){
  41. self.undelegate(buttonSp);
  42. context.start();
  43. });
  44. return true;
  45. }
  46. });
  47. var HelloWorldLayer = cc.Layer.extend({
  48. ctor:function () {
  49. this._super();
  50. this.setBackground('res/bg.jpg');
  51. var startLayer = new StartLayer(this);
  52. startLayer.attr('zIndex', 20);
  53. this.addChild(startLayer);
  54. this.startLayer = startLayer;
  55. return true;
  56. },
  57. start: function(){
  58. var self = this;
  59. this.startLayer.fadeOut(0.1).then(function(){
  60. self.startLayer.removeFromParent(true);
  61. }).act();
  62. this.initIcons();
  63. this.setNotice(10, 3);
  64. this.speedy = 1500;
  65. this.score = 0;
  66. this.tap = 0;
  67. var startTime = Date.now();
  68. setTimeout(function foo(){
  69. self.speedy = Math.max(self.speedy-5, 200);
  70. self.setNotice(5, 2);
  71. if(Date.now() - startTime <= 30000){
  72. setTimeout(foo, self.speedy);
  73. }else{
  74. self.timeout = true;
  75. self.checkend();
  76. }
  77. }, self.speedy);
  78. },
  79. checkend: function(){
  80. this.isGameOver = !this.icons.some(function(o){
  81. return o.notice;
  82. });
  83. if(this.isGameOver){
  84. //console.log('gameOver');
  85. this.gameOver();
  86. }
  87. },
  88. gameOver: function(){
  89. //console.log(this.score);
  90. var self = this;
  91. var i = 0;
  92. var hiscore = 635;
  93. var score = this.score;
  94. var rand = Math.random()*12454;
  95. var rank = 0|((hiscore - score) * 34763 + rand);
  96. var percent = (score *34763 + rand) / (hiscore*34763+rand);
  97. percent = Math.min(0.999, percent);
  98. dp_submitScore(this.score);
  99. var layerMask = cc.LayerColor.create(cc.color('rgba(0,0,0,128)'));
  100. layerMask.attr({
  101. zOrder: 88,
  102. opacity: 0
  103. });
  104. self.addChild(layerMask);
  105. layerMask.delay(0.5).then(function(){
  106. layerMask.attr('opacity', 192);
  107. var share = cc.createSprite('share_arraw.png', {
  108. anchor: [1.0, 1.0],
  109. xy: [620, 1136],
  110. opacity: 0,
  111. scale: 0.5
  112. });
  113. layerMask.addChild(share);
  114. var text = cc.tmpl("强迫症真的伤不起,你干掉了{score}个新提示。分享给你的强迫症好友吧。", {tap: self.tap, score: score, percent: (percent * 100).toFixed(1)});
  115. var result = cc.createSprite('@'+text, {
  116. xy: [320, 720],
  117. fontSize: 36,
  118. size: [600, 300],
  119. textAlign: 'center'
  120. });
  121. layerMask.addChild(result);
  122. var againButton = cc.Button.create('button_bg.png', {
  123. xy: [320, 560],
  124. opacity: 0,
  125. scale: 0.88
  126. });
  127. layerMask.addChild(againButton);
  128. var text = cc.createSprite('@再玩一次', {
  129. xy: [220, 40],
  130. fontSize: 46,
  131. color: '#00a538',
  132. });
  133. againButton.setCascadeOpacityEnabled(true);
  134. againButton.addChild(text);
  135. againButton.delay(0.5).fadeIn(0.5).act();
  136. layerMask.delegate(againButton, 'click', function(){
  137. layerMask.fadeOut(0.5).then(function(){
  138. self.getParent().reload();
  139. }).act();
  140. });
  141. var shareButton = cc.Button.create('button_bg.png', {
  142. xy: [320, 460],
  143. opacity: 0,
  144. scale: 0.88
  145. });
  146. layerMask.addChild(shareButton);
  147. text = cc.createSprite('@分享给好友', {
  148. xy: [220, 40],
  149. fontSize: 46,
  150. color: '#00a538',
  151. });
  152. shareButton.setCascadeOpacityEnabled(true);
  153. shareButton.addChild(text);
  154. shareButton.delay(0.6).fadeIn(0.5).act();
  155. layerMask.delegate(shareButton, 'click', function(){
  156. dp_share();
  157. });
  158. var otherButton = cc.Button.create('button_bg.png', {
  159. xy: [320, 360],
  160. opacity: 0,
  161. scale: 0.88
  162. });
  163. layerMask.addChild(otherButton);
  164. text = cc.createSprite('@更多游戏',{
  165. xy: [220, 40],
  166. fontSize: 46,
  167. color: '#00a538',
  168. });
  169. otherButton.setCascadeOpacityEnabled(true);
  170. otherButton.addChild(text);
  171. otherButton.delay(0.7).fadeIn(0.5).act();
  172. layerMask.delegate(otherButton, 'click', function(){
  173. clickMore();
  174. });
  175. }).act();
  176. },
  177. initIcons: function(){
  178. var self = this;
  179. this.icons = [];
  180. for(var i = 0; i < 16; i++){
  181. var sp = cc.createSprite(ICONS[i][0], {
  182. xy: [90 + (i % 4) * 152, 1026 - (0 | (i / 4)) * 176],
  183. opacity: 0
  184. });
  185. this.addChild(sp);
  186. sp.setCascadeOpacityEnabled(false);
  187. /*var spTxt = cc.createSprite('@'+ICONS[i][1], {
  188. xy: [80 + (i % 4) * 160, 915 - (0 | (i / 4)) * 190],
  189. fontSize: 22
  190. });
  191. this.addChild(spTxt);*/
  192. (function(sp){
  193. self.delegate(sp, {
  194. touchstart: function(){
  195. var mask = cc.createSprite('mask.png', {
  196. xy: [62, 61],
  197. });
  198. sp.addChild(mask);
  199. sp.mask = mask;
  200. if(sp.notice){
  201. self.score += sp.notice.value;
  202. self.tap++;
  203. sp.notice.removeFromParent(true);
  204. self.speedy -= 50;
  205. delete sp.notice;
  206. if(self.timeout){
  207. self.checkend();
  208. }
  209. }
  210. },
  211. touchend: function(){
  212. if(sp.mask){
  213. sp.mask.removeFromParent(true);
  214. delete sp.mask;
  215. }
  216. }
  217. });
  218. })(sp);
  219. this.icons.push(sp);
  220. }
  221. },
  222. setNotice: function(n, max){
  223. var icons = cc.random(this.icons, max);
  224. for(var i = 0; i < n; i++){
  225. var icon = cc.random(icons);
  226. if(!icon.notice){
  227. icon.notice = cc.createSprite("n.png", {
  228. xy: [115, 115],
  229. scale: 0.1,
  230. });
  231. icon.notice.value = 1;
  232. var v = cc.createSprite("@1", {
  233. xy: [20, 20],
  234. fontSize: 26
  235. });
  236. icon.notice.addChild(v);
  237. icon.notice.v = v;
  238. icon.addChild(icon.notice);
  239. icon.notice.scaleTo(0.2, 1.0).act();
  240. }else{
  241. icon.notice.value++;
  242. //icon.notice.attr('texture', 'n.png');
  243. icon.notice.v.setString(icon.notice.value);
  244. icon.notice.scaleTo(0.1, 1.3).scaleTo(0.1, 1.0).act();
  245. }
  246. }
  247. },
  248. backClicked: function(){
  249. }
  250. });
  251. var HelloWorldScene = cc.Scene.extend({
  252. onEnter:function () {
  253. this._super();
  254. var layer = new HelloWorldLayer();
  255. this.addChild(layer);
  256. }
  257. });