index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. var Main = {};
  2. btGame.makePublisher(Main);
  3. // 舞台宽高
  4. Main.width = 720;
  5. Main.height = 950;
  6. // 地平线
  7. Main.floorLine = 820;
  8. // 路标间距
  9. Main.guideDistance = Main.width;
  10. Main.debug = false;
  11. // 角度
  12. Main.minAngle = 5;
  13. Main.maxAngle = 45;
  14. Main.randomAngle = 2; // 上下偏移角度
  15. // 掉落时间
  16. Main.fallingTime = 1100;
  17. // 速度
  18. Main.speed = 300;
  19. // 舞台可视距离
  20. Main.visibleDistance = 250;
  21. // 最高份
  22. Main.maxScore = (window.localStorage && +localStorage["penguinMaxScore"]) || 0;
  23. // 帧频
  24. Main.fps = 40;
  25. var Resource;
  26. ;~function(res){
  27. // 资源加载
  28. res.load = function(){
  29. if(res.queue != null) return;
  30. res.queue = new createjs.LoadQueue(false);
  31. res.queue.addEventListener("progress",function(e) {
  32. //console.log(e);// 进度:e.loaded
  33. btGame.gameLoading(e.loaded);
  34. });
  35. res.queue.addEventListener("complete",function(e) {
  36. Main.log("完成", e);
  37. });
  38. res.queue.addEventListener("error",function(e) {
  39. btGame.gameLoading(0.1, e.text);
  40. });
  41. res.queue.loadFile("img/start.png");
  42. res.queue.loadFile("img/morebtn.png");
  43. res.queue.loadFile("img/startbtn.png");
  44. res.queue.loadFile("img/penguin.png");
  45. res.queue.loadFile("img/bear.png");
  46. res.queue.loadFile("img/hill.png");
  47. res.queue.loadFile("img/guide.png");
  48. res.queue.loadFile("img/background1.png");
  49. res.queue.loadFile("img/background2.png");
  50. res.queue.loadFile("img/flyend.png");
  51. res.queue.loadFile("img/traces.png");
  52. res.queue.loadFile("img/longtraces.png");
  53. res.queue.loadFile("img/score.png");
  54. res.queue.loadFile("img/fly.png");
  55. res.queue.loadFile("img/end.png");
  56. res.queue.loadFile("img/againbtn.png");
  57. res.queue.loadFile("img/notifybtn.png");
  58. }
  59. res.get = function(path){
  60. return res.queue.getResult(path);
  61. }
  62. }(Resource || (Resource = {}));
  63. Main.log = function(){
  64. Main.debug && console.log(arguments);
  65. }
  66. Main.shareTextMap = {
  67. "0": {
  68. tip: "没打中,手滑!再来一次~"
  69. ,title: "玩到了一个坑爹的游戏,不想砸手机就千万别来玩!"
  70. }
  71. ,"1": {
  72. tip: "没吃饱饭吗,用力用力!!"
  73. ,title: "我把企鹅打出了${m}米,击败了${n}%人,来挑战我吧!"
  74. }
  75. ,"2": {
  76. tip: "死鬼!想把我打去北极吗!"
  77. ,title: "我把企鹅打出了${m}米,击败了${n}%人,来挑战我吧!"
  78. }
  79. ,"3": {
  80. tip: "讨厌啦~怎么那么棒!"
  81. ,title: "我把企鹅打出了${m}米,击败了${n}%人,来挑战我吧!"
  82. }
  83. };
  84. Main.getShareText = function(distance){
  85. var i = 0, n = 0;
  86. if(distance > 0){
  87. i = 1;
  88. n = Math.floor(Math.random() * 11 + 70);
  89. }
  90. if(distance > 500){
  91. i = 2;
  92. n = Math.floor(Math.random() * 9 + 81);
  93. }
  94. if(distance > 900){
  95. i = 3;
  96. n = Math.floor(Math.random() * 9 + 90);
  97. }
  98. var share = Main.shareTextMap[i];
  99. btGame.setShare({
  100. title:share.title.replace("${m}", distance).replace("${n}", n)
  101. });
  102. return share;
  103. };
  104. // 游戏开始界面
  105. Main.startGame = function(){
  106. var container = new createjs.Container();
  107. container.addChild(new createjs.Bitmap(Resource.get("img/start.png")));
  108. // 开始游戏
  109. var beginGame = new createjs.Bitmap(Resource.get("img/startbtn.png"));
  110. beginGame.setTransform(34, 580);
  111. beginGame.cursor = "pointer";
  112. beginGame.addEventListener("click", function(){
  113. createjs.Tween.get(container, {loop: false})
  114. .to({alpha: 0}, 300)
  115. .call(function(){
  116. Main.initGame();
  117. });
  118. });
  119. container.addChild(beginGame);
  120. // 更多游戏
  121. var moreGame = new createjs.Bitmap(Resource.get("img/morebtn.png"));
  122. moreGame.setTransform(377, 580);
  123. moreGame.cursor = "pointer";
  124. moreGame.addEventListener("click", function(){
  125. clickMore();
  126. });
  127. container.addChild(moreGame);
  128. Main.stage.addChild(container);
  129. }
  130. // 游戏结束界面
  131. Main.endGame = function(distance){
  132. if(!Main.endContainer){
  133. Main.endContainer = new createjs.Container();
  134. }else{
  135. Main.endContainer.removeAllEventListeners();
  136. Main.endContainer.removeAllChildren();
  137. }
  138. var container = Main.endContainer;
  139. container.alpha = 0;
  140. container.addChild(new createjs.Bitmap(Resource.get("img/end.png")));
  141. // 本次成绩
  142. var text1 = new createjs.Text(
  143. "本次距离: "+distance+" M", "bold 28pt Tahoma Helvetica Arial sans-serif", "#ffffff"
  144. );
  145. text1.textAlign = "center";
  146. text1.x = Main.width / 2;
  147. text1.y = 174;
  148. container.addChild(text1);
  149. var text2 = text1.clone(true);
  150. text2.text = "最远距离: " + Main.maxScore + " M";
  151. text2.y = 248;
  152. container.addChild(text2);
  153. var share = Main.getShareText(distance);
  154. var text3 = text1.clone(true);
  155. text3.text = share["tip"];
  156. text3.font = "bold 24pt Tahoma Helvetica Arial sans-serif";
  157. text3.textAlign = "center";
  158. text3.y = 394;
  159. container.addChild(text3);
  160. // 再来一次
  161. var again = new createjs.Bitmap(Resource.get("img/againbtn.png"));
  162. again.setTransform(35, 623);
  163. again.addEventListener("click", function(){
  164. createjs.Tween.get(container, {loop: false})
  165. .to({y: -Main.height, alpha: 0}, 500, createjs.Ease.quintInOut)
  166. .call(function(){
  167. container.alpha = 1;
  168. container.removeAllEventListeners();
  169. container.removeAllChildren();
  170. Main.reset.replay();
  171. });
  172. });
  173. container.addChild(again);
  174. // 通知好友
  175. var notify = new createjs.Bitmap(Resource.get("img/notifybtn.png"));
  176. notify.setTransform(376, 623);
  177. notify.addEventListener("click", function(){
  178. dp_share();
  179. });
  180. container.addChild(notify);
  181. // 更多游戏
  182. var more = new createjs.Shape();
  183. more.graphics.beginFill("#ffffff").drawRect(0, 0, Main.width, 60);
  184. more.alpha = 0.1;
  185. more.y = 870;
  186. more.addEventListener("click", function(){
  187. clickMore();
  188. });
  189. container.addChild(more);
  190. Main.stage.addChild(container);
  191. container.y = -Main.height;
  192. createjs.Tween.get(container, {loop: false})
  193. .to({y:0, alpha:1}, 500, createjs.Ease.quintInOut);
  194. dp_submitScore(distance);
  195. }
  196. Main.initStage = function(){
  197. // 舞台已经有了,就不用再初始化了
  198. if(!this.stage){
  199. Main.stage = new createjs.Stage("canvas");
  200. }
  201. // Main.stage.enableMouseOver();
  202. Main.stage.removeAllEventListeners();
  203. Main.stage.removeAllChildren();
  204. // 清空舞台
  205. Main.stage.removeAllChildren();
  206. Main.stage.removeAllEventListeners();
  207. // 舞台大小
  208. Main.stage.width = canvas.width = Main.width;
  209. Main.stage.height = canvas.height = Main.height;
  210. createjs.Ticker.addEventListener("tick", this.stage);
  211. // 先加载资源
  212. Resource.load();
  213. Resource.queue.addEventListener("complete", function(){
  214. // Main.startGame();
  215. Main.startGame();
  216. });
  217. };
  218. Main.initGame = function(){
  219. Main.initGame = function(){}; // 防止重复初始化
  220. Main.stage.enableMouseOver(true);
  221. Main.log("开始初始化游戏");
  222. Main.log("初始化:游戏背景");
  223. var img = new createjs.Container();
  224. img.setBounds(0, 0, Main.width * 2, Main.height);
  225. var b1 = new createjs.Bitmap(Resource.get("img/background2.png"));
  226. img.addChild(b1);
  227. b1 = new createjs.Bitmap(Resource.get("img/background2.png"));
  228. b1.x = Main.width;
  229. img.addChild(b1);
  230. b1 = new createjs.Bitmap(Resource.get("img/background1.png"));
  231. b1.x = Main.width * 2;
  232. img.addChild(b1);
  233. this.background = img;
  234. this.stage.addChild(img);
  235. this.reset.background(img);
  236. Main.log("初始化:北极熊");
  237. var bearImg = new createjs.Bitmap(Resource.get("img/bear.png"));
  238. var bearBound = bearImg.getBounds();
  239. var bearSS = new createjs.SpriteSheet({
  240. "framerate": 4
  241. ,"animations":{
  242. "normal": [0, 0]
  243. ,"prepare": [1, 2, false, 1.5]
  244. ,"shoot": {
  245. frames: [1, 3, 4, 5],
  246. next: false,
  247. speed: 3
  248. }
  249. ,"shootNull":{
  250. frames: [1, 3, 4, 0],
  251. next: false,
  252. speed: 2
  253. }
  254. },
  255. "images": [Resource.get("img/bear.png")],
  256. "frames":{
  257. "height": 360,
  258. "width":300,
  259. "regX": 0,
  260. "regY": 0,
  261. "count": 6
  262. }
  263. });
  264. var bear = new createjs.Sprite(bearSS);
  265. this.bear = bear;
  266. this.stage.addChild(bear);
  267. this.reset.bear();
  268. this.knockPoint = Math.ceil(bear.y + bearBound.height * 0.1);
  269. if(Main.debug){
  270. Main.log("计算击打点:" + this.knockPoint);
  271. var shape = new createjs.Shape();
  272. shape.graphics.beginFill("#ff0000").drawRect(0, 0, 50, 10);
  273. shape.x = this.width - 50;
  274. shape.y = this.knockPoint;
  275. this.stage.addChild(shape);
  276. shape = null;
  277. // 地平线
  278. var floor = new createjs.Shape();
  279. floor.graphics.beginFill("#ffff00").drawRect(0, 0, Main.width, 10);
  280. floor.x = 0;
  281. floor.y = this.floorLine;
  282. this.stage.addChild(floor);
  283. floor = null;
  284. }
  285. // 回收熊的内存
  286. bearImg = bearBound = bearSS = bear = null;
  287. Main.log("初始化: 分数");
  288. var score = new createjs.Container();
  289. score.setBounds(0, 0, 238, 56);
  290. if(this.debug){
  291. var shape = new createjs.Shape();
  292. shape.graphics.beginFill("#fefefe").drawRect(
  293. 0, 0, score.getBounds().width, score.getBounds().height
  294. );
  295. score.addChild(shape);
  296. }
  297. var top = new createjs.Text("TOP:", "36px Arial Black", "#336600");
  298. score.addChild(top);
  299. var topWidth = top.getMeasuredWidth();
  300. var text = new createjs.Text("", "36px Arial Black", "#FF0000");
  301. text.x = topWidth;
  302. score.addChild(text);
  303. this.scoreTop = top;
  304. this.scoreText = text;
  305. this.score = score;
  306. this.stage.addChild(score);
  307. this.reset.score();
  308. score = top = text = null;
  309. // 路标
  310. Main.log("初始化:路标");
  311. var guideBackground = new createjs.Bitmap(Resource.get("img/guide.png"));
  312. var guideBackgroundHeight = guideBackground.getBounds().height;
  313. var guideBackgroundWidth = guideBackground.getBounds().width;
  314. var guideIcon = new createjs.Container();
  315. guideIcon.setBounds(guideBackground.getBounds());
  316. guideIcon.addChild(guideBackground);
  317. guideIcon.y = this.floorLine - guideBackgroundHeight - 20;
  318. var guideText = new createjs.Text("", "28px Arial Black", "#2B5580");
  319. guideText.y = 40;
  320. guideIcon.addChild(guideText);
  321. this.guideWidth = guideBackgroundWidth;
  322. this.guideHeight = guideBackgroundHeight;
  323. this.guideText = guideText;
  324. this.guide = guideIcon;
  325. this.stage.addChild(guideIcon);
  326. this.reset.guide("100M");
  327. Main.log("初始化:企鹅");
  328. var penguinImg = new createjs.Bitmap(Resource.get("img/penguin.png"));
  329. var penguinBound = penguinImg.getBounds();
  330. var penguinSS = new createjs.SpriteSheet({
  331. "framerate": 5
  332. ,"animations": {
  333. "normal": {
  334. frames:[0, 1, 0, 1, 2]
  335. ,next: false
  336. }
  337. ,"nod" : [2, 2, "normal"]
  338. ,"jump" : {
  339. frames: [2, 3, 4, 5]
  340. ,speed: 2
  341. ,next: false
  342. }
  343. }
  344. ,"images" : [Resource.get("img/penguin.png")]
  345. ,"frames" : {
  346. width: 180,
  347. height: 170,
  348. count: 6
  349. }
  350. });
  351. var penguin = new createjs.Sprite(penguinSS);
  352. this.penguin = penguin;
  353. this.penguinHeight = penguinBound.height;
  354. this.stage.addChild(penguin);
  355. this.reset.penguin();
  356. // 回收内存吧,少年!
  357. img = bound = null;
  358. Main.log("初始化:游戏结束的企鹅");
  359. var endData = {
  360. images: [Resource.get("img/flyend.png")],
  361. frames: {width:180, height:170, count: 2},
  362. animations: {slide:[0], down:[1]}
  363. };
  364. Main.gameOverPenguinSS = new createjs.SpriteSheet(endData);
  365. endData = null;
  366. // 结束的分数牌
  367. Main.log("初始化:游戏结束的分数牌");
  368. Main.gameOverScore = new createjs.Container();
  369. Main.gameOverScoreBg = new createjs.Bitmap(Resource.get("img/score.png"));
  370. Main.gameOverScore.addChild(Main.gameOverScoreBg);
  371. Main.gameOverScore.regY = Main.gameOverScoreBg.getBounds().height;
  372. Main.gameOverScoreText = new createjs.Text("0", "26px Arial Black", "#336600");
  373. Main.gameOverScoreText.x = 10;
  374. Main.gameOverScoreText.y = 33;
  375. Main.gameOverScore.addChild(Main.gameOverScoreText);
  376. Main.stage.addChild(Main.gameOverScore);
  377. Main.gameOverScore.x = 0;
  378. Main.gameOverScore.y = 0;
  379. Main.gameOverScore.cursor = "pointer";
  380. Main.reset.gameOverScore(1);
  381. // 事件绑定,htc 坑爹的速度
  382. setTimeout(function(){
  383. Main.initEvent();
  384. }, 200);
  385. };
  386. Main.gameOverSocreHd = {
  387. over: function(event){
  388. console.log(this.hover);
  389. }
  390. ,out: function(event){
  391. console.log(this.hover);
  392. }
  393. }
  394. // 重设状态
  395. Main.penguinOffsetX = 260;
  396. Main.reset = {
  397. replay: function(){
  398. this.guide("100M");
  399. this.score();
  400. this.penguin();
  401. this.bear();
  402. this.background();
  403. this.endPenguin(false);
  404. this.gameOverScore("0");
  405. // 游戏数据
  406. Main.penguinAnimation = null;
  407. Main.isPlaying = false; // 正在进行游戏
  408. Main.isQuiver = false;
  409. Main.guideMoveLength = 0;
  410. Main.guideMoveIndex = 1;
  411. Main.fire("replay");
  412. }
  413. ,guide: function(text){
  414. Main.guide.x = -Main.guideWidth;
  415. var guideText = Main.guideText;
  416. if(text){
  417. guideText.text = text;
  418. guideText.x = (Main.guideWidth - guideText.getMeasuredWidth()) / 2;
  419. }
  420. }
  421. ,score: function(txt){
  422. var text = Main.scoreText;
  423. var topWidth = Main.scoreTop.getMeasuredWidth(); // top 文字的宽度
  424. var maxWidth = Main.score.getBounds().width - topWidth;
  425. text.text = txt || (Main.maxScore + "M");
  426. var width = text.getMeasuredWidth();
  427. if(width >= maxWidth){
  428. text.x = topWidth;
  429. text.text = text.text.slice(0, -1);
  430. }else{
  431. text.x = topWidth + (maxWidth - width) / 2;
  432. }
  433. Main.score.x = 68;
  434. Main.score.y = 681;
  435. }
  436. ,gameOverScore: function(txt){
  437. var text = Main.gameOverScoreText;
  438. var offsetX = 11, width = 100;
  439. if(txt){
  440. text.text = txt;
  441. var textWidth = text.getMeasuredWidth();
  442. if(textWidth >= width){
  443. text.x = offsetX;
  444. }else{
  445. text.x = (width - textWidth) / 2 + offsetX;
  446. }
  447. }
  448. Main.gameOverScore.y = -Main.gameOverScore.getBounds().height;
  449. }
  450. ,penguin: function(){
  451. var penguin = Main.penguin;
  452. var bound = Main.penguin.getBounds();
  453. penguin.x = Main.width - Main.penguinOffsetX;
  454. penguin.y = 0;
  455. penguin.alpha = 1;
  456. penguin.rotation = 0;
  457. penguin.gotoAndPlay("normal");
  458. }
  459. ,bear: function(){
  460. var bear = Main.bear;
  461. var bound = bear.getBounds();
  462. bear.x = Main.width - 330;
  463. bear.y = 500;
  464. bear.gotoAndPlay("normal");
  465. }
  466. ,background: function(){
  467. var background = Main.background;
  468. Main.stopMoveStage();
  469. background.regX = 2 * Main.width;
  470. }
  471. ,endPenguin: function(visible){
  472. if(visible){
  473. if(!Main.gameOverPenguin){
  474. Main.gameOverPenguin = new createjs.Sprite(Main.gameOverPenguinSS);
  475. Main.stage.addChild(Main.gameOverPenguin);
  476. }
  477. Main.penguin.alpha = 0;
  478. Main.gameOverPenguin.alpha = 1;
  479. Main.gameOverPenguin.gotoAndPlay("down");
  480. Main.gameOverPenguin.x = Main.penguin.x;
  481. Main.gameOverPenguin.y = Main.penguin.y;
  482. }else{
  483. Main.penguin.alpha = 1;
  484. Main.gameOverPenguin && (Main.gameOverPenguin.alpha = 0);
  485. }
  486. }
  487. };
  488. Main.initEvent = function(){
  489. Main.clickTime = 0;
  490. this.stage.removeEventListener("stagemousedown", this.eventHandler.stageClick);
  491. this.stage.addEventListener("stagemousedown", this.eventHandler.stageClick);
  492. };
  493. var eventCD = false;
  494. Main.eventHandler = {
  495. stageClick: function(event){
  496. // 如果点击了游戏结束排
  497. // 三星坑爹的事件,重复触发了
  498. if(!eventCD){
  499. Main.fire("stageClick", Main.clickTime);
  500. eventCD = true;
  501. setTimeout(function(){
  502. eventCD = false;
  503. }, 500);
  504. }
  505. }
  506. };
  507. Main.penguinAnimation = null;
  508. Main.isPlaying = false; // 正在进行游戏
  509. Main.isQuiver = false; // 是否击中
  510. /* 事件处理 */
  511. Main.on("stageClick", function(clickTime){
  512. // 第一次点击:企鹅掉下来 + 白熊准备动作
  513. if(!Main.isPlaying){
  514. Main.isPlaying = true;
  515. // 重设场景
  516. this.reset.penguin();
  517. this.reset.bear();
  518. this.reset.background();
  519. this.reset.score();
  520. Main.log("白熊挥棒了!");
  521. Main.bear.gotoAndPlay("prepare");
  522. Main.log("企鹅跳下来吧~!");
  523. Main.penguin.gotoAndPlay("jump");
  524. var toY = Main.floorLine - Main.penguinHeight + 70;
  525. Main.penguinAnimation = createjs.Tween.get(this.penguin, {loop: false});
  526. Main.penguinAnimation.to({y: toY}, Main.fallingTime, createjs.Ease.quadIn)
  527. .call(function(){
  528. Main.fire("gameOver", 0);
  529. Main.log("掉下来了");
  530. });
  531. // 监听熊的挥棒动作
  532. Main.bearListenerShoot();
  533. }else if(!Main.penguinAnimation._paused){
  534. // 这里很诡异,没有检测 pause 的方法..
  535. var penguinY = this.penguin.y - this.penguin.regY;
  536. var clickX = Math.abs(this.knockPoint - penguinY);
  537. Main.isQuiver = clickX <= this.penguinHeight;
  538. // 击中了?
  539. if(Main.isQuiver){
  540. clickX = Math.ceil(clickX / this.penguinHeight * 100);
  541. var angle = Main.calculateAngle(clickX);
  542. Main.log("击中角度:" + angle);
  543. Main.fire("knockPenguin", angle);
  544. // 解除熊的监听
  545. Main.bearRemoveListenerShoot();
  546. }else{
  547. Main.log("你木有击中!!");
  548. }
  549. this.bear.gotoAndPlay("shoot");
  550. }
  551. });
  552. Main.bearListenerShoot = function(){
  553. this.bearRemoveListenerShoot();
  554. this.bear.addEventListener("animationend", Main.bearShoot);
  555. }
  556. Main.bearRemoveListenerShoot = function(){
  557. this.bear.removeEventListener("animationend", Main.bearShoot);
  558. }
  559. // 如果打不中,立刻回去准备
  560. Main.bearShoot = function(){
  561. if(!Main.isQuiver && Main.bear.currentAnimation == "shoot"){
  562. Main.bear.gotoAndPlay("prepare");
  563. }
  564. }
  565. Main.on("knockPenguin", function(event, angle){
  566. Main.penguinAnimation.setPaused(true);
  567. Main.flyLine(angle);
  568. });
  569. Main.on("moveStage", function(event, distance, totalDistance){
  570. Main.moveStage(distance, totalDistance);
  571. });
  572. Main._moveStage = function(distance, totalDistance){
  573. Main.moveStage = Main.moveStage2;
  574. Main.moveStage(distance, totalDistance);
  575. }
  576. Main.moveStage = Main._moveStage;
  577. Main.stopMoveStage = function(){
  578. // this.stage.removeEventListener("tick", testMoveStage);
  579. Main.moveStage = Main._moveStage;
  580. }
  581. // 2、熊、山都和背景,都往后挪,Main.visibleDistance距离左右,就不挪动了
  582. Main.moveStage2 = function(distance, totalDistance){
  583. if(this.background.regX > Main.width){
  584. this.bear.x += distance;
  585. this.score.x += distance;
  586. this.background.regX -= distance;
  587. Main.moveGuide(distance, totalDistance);
  588. }else{
  589. Main.moveStage = Main.moveStage3;
  590. // Main.guideMoveLength = Main.guideDistance + Main.guideWidth;
  591. Main.moveStage3(distance, totalDistance);
  592. // 添加1个路标
  593. // Main.tool.addNewGuide(totalDistance, 0);
  594. }
  595. }
  596. // 3、背景往后挪
  597. Main.moveStage3 = function(distance, totalDistance){
  598. this.background.regX -= distance;
  599. if(this.background.regX <= 0){
  600. this.background.regX += Main.width;
  601. }
  602. Main.moveGuide(distance, totalDistance);
  603. }
  604. // 路标
  605. Main.guideMoveLength = 0;
  606. Main.guideMoveIndex = 1;
  607. Main.moveGuide = function(distance, totalDistance){
  608. this.guide.x += distance;
  609. // 路标
  610. this.guideMoveLength += distance;
  611. if(this.guideMoveLength >= this.guideDistance + this.guideWidth){
  612. this.reset.guide(++Main.guideMoveIndex + "00M");
  613. this.guideMoveLength = 0;
  614. }
  615. }
  616. Main.getTotalDistance = function(distance){
  617. Main.log(distance);
  618. var result = Main.guideMoveIndex * 100;
  619. var total = this.guideDistance + this.guideWidth;
  620. result += Math.round(Main.guide.x * total / Main.width) / 10;
  621. return distance > 0 ? result : 0;
  622. }
  623. Main.calculateAngle = function(x){
  624. Main.log("角度" + x);
  625. var y = Main.minAngle + Main.maxAngle * Math.sin(Math.PI * x / 200);
  626. y += Math.round(Math.random() * Main.randomAngle) * (Math.random() < 0.5 ? -1 : 1);
  627. return Math.ceil(y);
  628. }
  629. Main.gameOverScoreUtil = {
  630. dropDown: function(score, x){
  631. Main.reset.gameOverScore(score);
  632. Main.gameOverScore.x = x;
  633. createjs.Tween.get(Main.gameOverScore, {loop: false})
  634. .to({
  635. y: Main.floorLine - 60
  636. }, 800, createjs.Ease.quintIn);
  637. }
  638. };
  639. // 游戏结束
  640. Main.on("gameOver", function(event, distance, x){
  641. Main.log("游戏结束:" + Main.getTotalDistance(distance));
  642. var score = Main.getTotalDistance(distance);
  643. var hasNewMaxScore = score > Main.maxScore;
  644. Main.maxScore = Math.max(Main.maxScore, score);
  645. try{
  646. localStorage["penguinMaxScore"] = Main.maxScore;
  647. }catch(e){
  648. }
  649. if(distance <= 0){
  650. Main.reset.endPenguin(true);
  651. Main.bear.gotoAndPlay("shootNull");
  652. //Main.gameOverScoreUtil.dropDown("0", Main.penguin.x + 50);
  653. }else{
  654. //Main.gameOverScoreUtil.dropDown(score, x);
  655. }
  656. setTimeout(function(){
  657. Main.endGame(score);
  658. if(hasNewMaxScore && score > 700){
  659. btGame.playScoreMsg("笨熊喝大力,大力出奇迹!你获得了最好的成绩,快跟好友分享一下吧");
  660. btGame.setShare({
  661. title: "我在《打企鹅》中将企鹅打出了"+Main.maxScore+"米,简直停不下来!"
  662. });
  663. }
  664. }, 1200);
  665. });
  666. btGame.resizePlayArea($("#container"), Main.width, Main.height, "center", "center");
  667. Main.initStage();