Guest User

Untitled

a guest
May 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.92 KB | None | 0 0
  1. !function(){
  2. var IS_IOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
  3. var IS_ANDROID = /android/i.test(navigator.userAgent);
  4. var IS_TOUCH = createjs.Touch.isSupported();
  5. var SCREEN_SHOW_ALL = true;
  6. var width = 640;
  7. var height = 1000;
  8. var stage, queue;
  9. var isMove = 0; //qp_o
  10. var score = 0;
  11. var playerItem = 3; // qp_e
  12. var playerIndex = 3; //qp_f
  13. var items = []; //qp_i
  14. var flyMoney = [];
  15. var timer = 15;
  16. var timeStart = -1;
  17. var isGameOver = false;
  18.  
  19. window.onload = function() {
  20. stage = new createjs.Stage('stage');
  21. queue = new createjs.LoadQueue(false, null, true);
  22. queue.setMaxConnections(30);
  23. if (IS_TOUCH) {
  24. createjs.Touch.enable(stage, true);
  25. stage.mouseEnabled = false;
  26. var shape = new createjs.Shape;
  27. shape.graphics.f("white").r(0, 0, width, height);
  28. stage.addChild(shape);
  29. }
  30. createjs.Ticker.setFPS(60);
  31. setTimeout(resize, 100)
  32. createjs.Ticker.on("tick", stage);
  33. queue.on('complete', config.startFunc, null, true);
  34. config.img && queue.loadManifest(config.img, false);
  35. queue.load();
  36. }
  37.  
  38. function resize() {
  39. var canvas = stage.canvas;
  40. var innerWidth = window.innerWidth;
  41. var innerHeight = window.innerHeight;
  42.  
  43. if (SCREEN_SHOW_ALL) {
  44. if (innerWidth / innerHeight > width / height) {
  45. innerWidth = width * innerHeight / height;
  46. } else {
  47. innerHeight = height * innerWidth / width;
  48. }
  49. canvas.style.marginTop = 0;
  50. } else {
  51. var temp = (width * innerHeight / height);
  52. if (innerWidth >= temp ){
  53. innerWidth = temp;
  54. stage.x = 0;
  55. } else {
  56. stage.x = (innerWidth - temp) / 2
  57. }
  58. }
  59. canvas.width = width;
  60. canvas.height = height;
  61. canvas.style.width = innerWidth + 'px';
  62. canvas.style.height = window.innerHeight + 'px';
  63. $('main').width(innerWidth);
  64. }
  65.  
  66. window.onresize = resize
  67.  
  68. function genRandom(value) {
  69. return parseInt(Math.random() * value)
  70. }
  71.  
  72. var _extends = function(ChildClass, ParentClass) {
  73. var f = function() { };
  74. f.prototype = ParentClass.prototype;
  75. for(var m in ChildClass.prototype) {
  76. f.prototype[m] = ChildClass.prototype[m];
  77. }
  78.  
  79. ChildClass.prototype = new f();
  80. ChildClass.prototype.constructor = ChildClass;
  81. ChildClass.prototype._super = ParentClass.prototype;
  82. }
  83.  
  84. function gameStart() {
  85. stage.arrow.visible = true;
  86. //stage.splash.visible = false;
  87. score = 0;
  88. timeStart = -1;
  89. isMove = 1;
  90. timer = 30;
  91. }
  92.  
  93. function splashContainer() {
  94. this.initialize();
  95. this.bg = new createjs.Shape;
  96. var bgImg = queue.getResult("bg");
  97.  
  98. var scale = width / bgImg.width;
  99. var matrix = new createjs.Matrix2D();
  100. matrix.scale(scale, scale);
  101. this.bg.graphics.beginFill('#00A0FF').drawRect(0, 0, width, 1200);
  102. this.bg.graphics.beginBitmapFill(bgImg, 'no-repeat', matrix).drawRect(0, 0, bgImg.width, bgImg.height);
  103. this.addChild(this.bg)
  104. var arrow = queue.getResult("upslide");
  105. this.upslide = new createjs.Bitmap(arrow)
  106. this.upslide.x = wihdth = (width - arrow.width) / 2;
  107. this.upslide.y = height - 500;
  108. this.addChild(this.upslide)
  109. this.txt = new createjs.Text('向上滑动开始游戏', "bold 32px Arial", "white");
  110. this.txt.textAlign = "center";
  111. this.txt.textBaseline = "middle";
  112. this.txt.x = width / 2;
  113. this.txt.y = height - 380;
  114. this.addChild(this.txt)
  115. var m0 = queue.getResult("mb0");
  116. this.start = new createjs.Bitmap(m0);
  117. this.start.y = height - 300;
  118. this.start.x = (width - m0.width) / 2;
  119. this.addChild(this.start);
  120. var localY, relateY;
  121. this.start.on("mousedown", function (event) {
  122. if (!isMove) {
  123. localY = event.localY;
  124. relateY = height - 300;
  125. }
  126. });
  127. this.start.on("pressmove", function (event) {
  128. if (!isMove) {
  129. splashMoveEvent(event.localY - localY, relateY)
  130. }
  131. });
  132. this.start.on("pressup", function (event) {
  133. if (!isMove && localY - event.localY < 30) {
  134. createjs.Tween.get(stage.splash.start).to({
  135. y: -height
  136. }, 400).call(function (a) {
  137. stage.splash.upslide.visible = false;
  138. gameStart();
  139. stage.splash.visible = false;
  140. })
  141. }
  142. $('.link-btn').hide()
  143. })
  144. var y = this.upslide.y;
  145. createjs.Tween.get(this.upslide, {loop: true}).to({
  146. y: y + 10,
  147. opacity: 0.5
  148. },
  149. 500).to({
  150. y: 10,
  151. opacity: 1
  152. }, 0);
  153. }
  154. _extends(splashContainer, createjs.Container);
  155.  
  156. function splashMoveEvent(a, b) {
  157. stage.splash.start.y + a < b && (stage.splash.start.y += a)
  158. }
  159.  
  160. function playerContainer() {
  161. this.initialize();
  162. var mb = queue.getResult('mb0');
  163. this.mb = new createjs.Bitmap(mb);
  164. this.mb.regX = mb.width / 2;
  165. this.mb.regY = mb.height / 2;
  166. this.mb.y = 420;
  167. this.x = width / 2;
  168. this.y = height / 2 - 150;
  169. this.addChild(this.mb);
  170. var m0 = queue.getResult('m0');
  171. this.m = [];
  172. for (var i = 0; i <= playerItem; i++) {
  173. this.m[i] = new createjs.Bitmap(m0);
  174. this.m[i].regX = m0.width / 2;
  175. this.m[i].regY = m0.height / 2;
  176. this.m[i].y = 420;
  177. this.m[i].visible = false;
  178. this.addChild(this.m[i]);
  179. }
  180. }
  181. _extends(playerContainer, createjs.Container);
  182.  
  183. playerContainer.prototype.playAnimation = function (a) {
  184. a.visible = !0;
  185. createjs.Tween.get(a).to({
  186. scaleX: 0.5,
  187. scaleY: 0.5,
  188. y: -height
  189. },
  190. 300).to({
  191. visible: !1,
  192. y: 420,
  193. scaleX: 1,
  194. scaleY: 1
  195. }, 0);
  196. if (playerItem > 0) {
  197. playerItem--;
  198. } else {
  199. playerItem = 3;
  200. }
  201. }
  202.  
  203. function startContainer() {
  204. var shape = new createjs.Shape;
  205. shape.graphics.beginFill('#00A0FF').drawRect(0, 0, width, height);
  206. stage.addChild(shape);
  207. var hitShape = new createjs.Shape;
  208. hitShape.graphics.beginFill("white").rect(0, 200, width, height);
  209. shape.hitArea = hitShape;
  210.  
  211. for (var index = 0; index <= playerItem; index++) {
  212. flyMoney[index] = [];
  213. for (var j = 0; j < 5; j++) {
  214. var d0 = queue.getResult("d0");
  215. var e = new createjs.Bitmap(d0);
  216. e.regX = d0.width / 2;
  217. e.regY =d0.height / 2;
  218. e.x = genRandom(width);
  219. e.y = - height / 2 + genRandom(height);
  220. e.visible = !1;
  221. flyMoney[index].push(e);
  222. stage.addChild(flyMoney[index][j])
  223. }
  224. }
  225. bindEvent(shape);
  226. stage.player = new playerContainer;
  227. stage.addChild(stage.player);
  228. var arrow = queue.getResult("upslide");
  229. stage.arrow = new createjs.Bitmap(arrow)
  230. stage.arrow.x = wihdth = (width - arrow.width) / 2;
  231. stage.arrow.y = 280;
  232. stage.arrow.visible = false
  233. stage.addChild(stage.arrow)
  234. stage.timer = new timerContainer;
  235. stage.addChild(stage.timer);
  236. stage.score = new scoreContainer;
  237. stage.addChild(stage.score);
  238.  
  239.  
  240. stage.splash = new splashContainer;
  241. setInterval(flyMoneyAnimation, 1000);
  242. stage.addChild(stage.splash)
  243. createjs.Ticker.addEventListener("tick", function(a) {
  244. stage.score.score.text = "\uffe5" + score
  245. if (timeStart >= 0) {
  246. timeStart += a.delta
  247. a = 15 - parseInt(timeStart / 1000)
  248. if (a != timer) {
  249. timer = a >= 0 ? a : 0;
  250. stage.timer.txt.text = timer + '"';
  251. if(timer <= 0) {
  252. timeStart = -1
  253. isGameOver = true;
  254. gameOver();
  255. console.log('gameover')
  256. }
  257. }
  258.  
  259. }
  260. })
  261.  
  262. $('.link-btn').show()
  263. //stage.splash = new splashContainer
  264. }
  265.  
  266. var fIndex = 0;
  267. function flyMoneyAnimation() {
  268. for (var index = 0; index < 5; index++) {
  269. flyMoney[fIndex][index].visible = true;
  270. createjs.Tween.get(flyMoney[fIndex][index]).to({
  271. y: height + flyMoney[fIndex][index].getBounds().height / 2 + 100,
  272. rotation: 720 + genRandom(400),
  273. x: genRandom(width)
  274. },
  275. 1000 + genRandom(800)).to({
  276. visible: !1
  277. },
  278. 10).to({
  279. x: genRandom(width),
  280. y: -height / 2 + genRandom(height / 2),
  281. rotation: 0
  282. },
  283. 10);
  284. }
  285. if (fIndex < 3) {
  286. fIndex++;
  287. } else {
  288. fIndex = 0;
  289. }
  290.  
  291. }
  292.  
  293. function scoreContainer() {
  294. this.initialize();
  295. var bg = queue.getResult('bg_money');
  296. this.scoreBg = new createjs.Bitmap(bg);
  297. this.scoreBg.x = (width - bg.width) / 2;
  298. this.scoreBg.y = 32;
  299. this.addChild(this.scoreBg);
  300. this.score = new createjs.Text("\uffe5" + score, "bold 42px Arial", "yellow");
  301. this.score.textAlign = "center";
  302. this.score.textBaseline = "middle";
  303. this.score.x = width / 2;
  304. this.score.y = this.scoreBg.y + bg.height / 2;
  305. this.addChild(this.score)
  306. }
  307.  
  308. _extends(scoreContainer, createjs.Container);
  309.  
  310. function timerContainer() {
  311. this.initialize();
  312. var bg = queue.getResult('bg_time');
  313. this.timeBg = new createjs.Bitmap(bg);
  314. this.timeBg.x = (width - bg.width) / 2;
  315. this.timeBg.y = 150;
  316. this.addChild(this.timeBg);
  317. var timeIcon = queue.getResult('icon_time');
  318. this.timeIcon = new createjs.Bitmap(timeIcon);
  319. this.timeIcon.x = this.timeBg.x + 15;
  320. this.timeIcon.y = this.timeBg.y + 18;
  321. this.addChild(this.timeIcon)
  322. this.txt = new createjs.Text('15"', "bold 44px Arial", "white");
  323. this.txt.textAlign = "center";
  324. this.txt.textBaseline = "middle";
  325. this.txt.x = width / 2 + timeIcon.width / 2;
  326. this.txt.y = 192;
  327. this.addChild(this.txt)
  328.  
  329. }
  330.  
  331. _extends(timerContainer, createjs.Container);
  332.  
  333.  
  334. function bindEvent(shape) {
  335. var c = 0;
  336. var d = 0;
  337. shape.on("mousedown", function(a) {
  338. if (!(IS_TOUCH && event.nativeEvent instanceof MouseEvent)) {
  339. if (2 != isMove || 1 != isMove) {
  340. c = a.localY;
  341. d = stage.player.m[playerItem].y;
  342. }
  343. }
  344. });
  345. shape.on("pressmove", function(event) {
  346. if (!(IS_TOUCH && event.nativeEvent instanceof MouseEvent)) {
  347. if (isMove == 1) {
  348. stage.arrow.visible = false
  349. timeStart = 0;
  350. isMove = 2
  351. }
  352. if (isMove == 2 && !isGameOver) {
  353. stage.player.m[playerItem].visible = true
  354. stage.player.m[playerItem].y += (event.localY - c) / 1.5
  355. }
  356. }
  357. });
  358. var f = 0;
  359. shape.on('pressup', function (event) {
  360. if (!(IS_TOUCH && event.nativeEvent instanceof MouseEvent)) {
  361. //console.log(gameOver)
  362. if (isMove == 2 && !isGameOver) {
  363. if (c - event.localY > 50) {
  364. var now = (new Date).getTime();
  365. if (items.length > 0 && items[items.length - 1] + 50 > now) {
  366. console.log('too fast')
  367. } else {
  368. f = formatTime(now)
  369. if (f <= 20) {
  370. score += 100;
  371. stage.player.playAnimation(stage.player.m[playerItem]);
  372. } else {
  373. items.length--
  374. }
  375. }
  376. } else {
  377. var b = Math.abs(stage.player.m[playerItem] - d);
  378. createjs.Tween.get(stage.player.m[playerItem]).to({y: d}, 20 * b)
  379. stage.player.m[playerItem].visible = false
  380. }
  381. }
  382. }})
  383. }
  384.  
  385.  
  386. function formatTime(time) {
  387. var b = 0;
  388. if (0 != items.length) {
  389. var c;
  390. for (c = 0; c < items.length && !(items[c] > time - 1000); c++);
  391. b = items.length - c
  392. for (var d = c; d < items.length; d++) {
  393. items[d - c] = items[d];
  394. }
  395. items.length -= c;
  396. }
  397. items.push(time);
  398. return parseInt(b)
  399. }
  400.  
  401. var $gameOverBox = $('.game-over-box');
  402. function gameOver() {
  403. $gameOverBox.show()
  404. uploadData()
  405. }
  406.  
  407. $('.restart-btn').on('click', function() {
  408. $gameOverBox.hide();
  409. score = 0;
  410. timer = 15;
  411. isMove = 1;
  412. isGameOver = false;
  413. stage.arrow.visible = true;
  414. //timeStart = 0;
  415. stage.timer.txt.text = timer + '"'
  416. })
  417.  
  418. function uploadData() {
  419. $.ajax({
  420. url: '/mkt/uploadGameResult/',
  421. type: 'POST',
  422. data: {openId: window.__openId, score: score},
  423. success: function(response) {
  424. addRankList(response)
  425. }
  426. })
  427. }
  428.  
  429. var $box = $('.billboard')
  430. function addRankList(response) {
  431. $result = [];
  432. var html = '';
  433. if (response.top) {
  434. var count = response.top.length > 3 ? 3 : response.top.length;
  435. for (var i = 0; i < count - 1; i++) {
  436. var avatar = response.top[i].headImgUrl;
  437. var name = response.top[i].name;
  438. html = '<li class="item-'+ i + '">'
  439. + '<div class="tag-box"><span class="tag-'+ i +'"></span></div>'
  440. + '<span class="avatar">'
  441. + '<img src="'+ avatar +'"/>'
  442. + '</span>'
  443. + '<span class="name">'+ name+ '</span>'
  444. + '</li>'
  445. //console.log(html);
  446. $result.push(html);
  447. }
  448. if ($result.length > 0) {
  449. $result.push('<div class="separator">●●●●●●</div>')
  450. }
  451. if (response.self) {
  452. var selfAvatar = response.self.headImgUrl;
  453. var selfName = response.self.name;
  454. var rank = response.self.rank.toString()
  455. var temp = '<div class="self"><li><div class="tag-box"><span>'+rank+'</span></div>'
  456. + '<span class="avatar">'
  457. + '<img src="'+ selfAvatar +'"/>'
  458. + '</span>'
  459. + '<span class="name">'+ selfName + '</span>'
  460. + '</li></div>'
  461. $result.push(temp);
  462. }
  463. $box.html($($result.join('')));
  464. }
  465.  
  466. }
  467. var $shareBg = $('.share-bg');
  468. var $shareLayer = $('.share-layer')
  469. $('.share-btn').on('click', function (event) {
  470. $shareBg.show();
  471. $shareLayer.show();
  472. var data = window.__weichatData;
  473. data['title'] = '我数了'+ score +'元手都抽筋了!来和我一起挑战数钱吧~';
  474. wxShare.all(data);
  475. wxShare.on('baixing', data);
  476. })
  477.  
  478. $shareBg.on('click', function () {
  479. $shareBg.hide();
  480. $shareLayer.hide();
  481. })
  482.  
  483. var config = {
  484. startFunc: startContainer,
  485. img: {
  486. path: '//s.baixing.net/img/activity/money/',
  487. manifest: [{
  488. src: 'bg.png',
  489. id: 'bg'
  490. },{
  491. src: 'arrow.png',
  492. id: 'upslide'
  493. }, {
  494. src: 'mb0.png',
  495. id: 'mb0'
  496. },{
  497. src: 'm0.png',
  498. id: 'm0'
  499. },{
  500. src: 'd0.png',
  501. id: 'd0'
  502. },{
  503. src: 'icon_time.png',
  504. id: 'icon_time'
  505. },{
  506. src: 'img_bg_time.png',
  507. id: 'bg_time'
  508. },{
  509. src: 'img_bg_money.png',
  510. id: 'bg_money'
  511. }]
  512. }
  513. }
  514. } ()
Add Comment
Please, Sign In to add comment