main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. var game = new Phaser.Game(320, 480, Phaser.AUTO, "game_div"),
  2. highScore = localStorage["avoiderHighScore"]?localStorage["avoiderHighScore"]:0,
  3. FONTFAMILY = "'Arial','Microsoft YaHei','黑体','宋体','sans-serif'";
  4. var preloader_state = {
  5. preload: function() {
  6. this["game"]["stage"]["scaleMode"] = Phaser["StageScaleMode"]["SHOW_ALL"];
  7. this["game"]["stage"]["scale"]["minWidth"] = 160;
  8. this["game"]["stage"]["scale"]["minHeight"] = 240;
  9. // a_why
  10. this["game"]["stage"]["scale"]["maxWidth"] = 960;
  11. this["game"]["stage"]["scale"]["maxHeight"] = 1440;
  12. this["game"]["stage"]["scale"]["pageAlignHorizontally"] = true;
  13. this["game"]["stage"]["scale"]["pageAlignVertically"] = true;
  14. this["game"]["stage"]["scale"]["setScreenSize"](true);
  15. this["game"]["stage"]["backgroundColor"] = "#0095de";
  16. this["game"]["load"]["image"]("preloaderbar", "assets/_loading.png");
  17. this["game"]["load"]["image"]("splash", "assets/_splash.png");
  18. },
  19. create: function() {
  20. this["game"]["state"]["start"]("loading");
  21. },
  22. update: function() {}
  23. };
  24. var loading_state = {
  25. preload: function() {
  26. this["game"]["stage"]["backgroundColor"] = "#0095de";
  27. this["game"]["add"]["sprite"](0, 0, "splash");
  28. this["preloadBar"] = this["add"]["sprite"](75, 230, "preloaderbar");
  29. this["load"]["setPreloadSprite"](this["preloadBar"]);
  30. this["game"]["load"]["image"]("collect", "assets/_collect.png");
  31. this["game"]["load"]["image"]("avoid", "assets/_avoid.png");
  32. this["game"]["load"]["image"]("move", "assets/_move.png");
  33. this["game"]["load"]["image"]("gameover", "assets/_gameover.png");
  34. this["game"]["load"]["image"]("playagain", "assets/_playagain.png");
  35. this["game"]["load"]["image"]("playsharetips", "assets/_share.png");
  36. this["game"]["load"]["image"]("getmoregame", "assets/_moregame.png");
  37. this["game"]["load"]["image"]("doudouin", "assets/_doudouin.png");
  38. this["game"]["load"]["image"]("logo", "assets/_logo.png");
  39. this["game"]["load"]["image"]("help", "assets/_text.png");
  40. this["game"]["load"]["image"]("play", "assets/_play.png");
  41. this["game"]["load"]["image"]("collect2", "assets/_collect2.png");
  42. this["game"]["load"]["image"]("scoreabatter", "assets/_scoreabattre.png");
  43. },
  44. create: function() {
  45. this["game"]["state"]["start"]("start");
  46. },
  47. update: function() {}
  48. };
  49. var start_state = {
  50. preload: function() {
  51. this["game"]["stage"]["backgroundColor"] = "#0b70b7";
  52. },
  53. create: function() {
  54. this["logo"] = this["game"]["add"]["sprite"](35, 15, "logo");
  55. this["help"] = this["game"]["add"]["sprite"](30, 100, "help");
  56. this["play"] = this["game"]["add"]["sprite"](80, 380, "play");
  57. this["game"]["input"]["onTap"]["add"](this["tapped"], this);
  58. },
  59. update: function() {},
  60. tapped: function() {
  61. game["state"]["start"]("main");
  62. }
  63. };
  64. var main_state = {
  65. copy: "COPYRIGHT (c)2014 James Kayes",
  66. score: 0,
  67. collected: 0,
  68. lastP: {x:-1, y:-1},
  69. canMove: false,
  70. textStyle: {
  71. font: "bold 32px "+FONTFAMILY,
  72. fill: "white"
  73. },
  74. preload: function() {
  75. this["score"] = 0;
  76. this["collected"] = 0;
  77. this["collect2"] = null;
  78. this["collect2"] = this["game"]["add"]["sprite"](Math["random"]() * 270, Math["random"]() * 240, "collect2");
  79. this["collect2"]["kill"]();
  80. },
  81. create: function() {
  82. this["balls"] = this["game"]["add"]["group"]();
  83. this["dragger"] = this["game"]["add"]["sprite"](138, 335, "move");
  84. this["diaY"] = this["game"]["add"]["sprite"](140, 200, "collect");
  85. this["ball1"] = this["balls"]["create"](Math["random"]() * 290, Math["random"]() * 450, "avoid");
  86. // this["dragger"]["inputEnabled"] = true;
  87. // this["dragger"]["input"]["enableDrag"](true);
  88. var that = this;
  89. this.resetTouchPosition();
  90. // if (this["game"]["input"]["touch"]["touchStartCallback"] == null) {
  91. this["game"]["input"]["touch"]["touchStartCallback"] = function() {
  92. that.resetTouchPosition();
  93. that.canMove = true;
  94. }
  95. this["game"]["input"]["touch"]["touchMoveCallback"] = function(e) {
  96. if (that.canMove) {
  97. if (!(that.lastP.x == -1 && that.lastP.y == -1)) {
  98. that["dragger"]["x"] += (e.touches[0].clientX-that.lastP.x);
  99. that["dragger"]["y"] += (e.touches[0].clientY-that.lastP.y);
  100. }
  101. that.lastP.x = e.touches[0].clientX;
  102. that.lastP.y = e.touches[0].clientY;
  103. }
  104. }
  105. this["game"]["input"]["touch"]["touchEndCallback"] = function() {
  106. that.resetTouchPosition();
  107. }
  108. // }
  109. // this["game"]["input"]["onDown"]["add"](this["downed"], this);
  110. // this["game"]["input"]["onUp"]["add"](this["resetTouchPosition"], this);
  111. // setTimeout(function() {
  112. // that["game"]["input"]["moveCallback"] = function(e) {
  113. // that["moveDragger"].call(that, e);
  114. // }
  115. // }, 10);
  116. this["ball1"]["body"]["velocity"]["setTo"](60, 60);
  117. this["ball1"]["body"]["collideWorldBounds"] = true;
  118. this["ball1"]["body"]["bounce"]["setTo"](1, 1);
  119. this["s"] = this["game"]["add"]["text"](0, 0, "分数: " + this["score"], this["textStyle"]);
  120. this["b"] = this["game"]["add"]["text"](0, 35, "记录: " + highScore, {
  121. font: "bold 24px "+FONTFAMILY,
  122. fill: "white"
  123. });
  124. },
  125. resetTouchPosition: function() {
  126. this.lastP.x = -1;
  127. this.lastP.y = -1;
  128. this.canMove = false;
  129. },
  130. moveDragger: function(e) {
  131. if (this.canMove) {
  132. if (!(this.lastP.x == -1 && this.lastP.y == -1)) {
  133. this["dragger"]["x"] += (e.x-this.lastP.x);
  134. this["dragger"]["y"] += (e.y-this.lastP.y);
  135. }
  136. this.lastP.x = e.x;
  137. this.lastP.y = e.y;
  138. }
  139. },
  140. downed: function(e) {
  141. this.lastP.x = -1;
  142. this.lastP.y = -1;
  143. this.canMove = true;
  144. },
  145. uped: function(e) {
  146. this.lastP.x = -1;
  147. this.lastP.y = -1;
  148. this.canMove = false;
  149. },
  150. update: function() {
  151. this["game"]["physics"]["collide"](this["dragger"], this["diaY"], this["collisionHandler"], null, this);
  152. this["game"]["physics"]["collide"](this["dragger"], this["balls"], this["ballcollisionHandler"], null, this);
  153. this["game"]["physics"]["collide"](this["dragger"], this["collect2"], this["collect2collision"], null, this);
  154. this["game"]["physics"]["collide"](this["diaY"], this["collect2"], this["collect2respawn"], null, this);
  155. if ((this["dragger"]["body"]["x"] > 289) || (this["dragger"]["body"]["x"] < 0) || (this["dragger"]["body"]["y"] > 449) || (this["dragger"]["body"]["y"] < 0)) {
  156. // this.resetTouchPosition();
  157. game["state"]["start"]("lose");
  158. };
  159. },
  160. collisionHandler: function() {
  161. if ((this["dragger"]["body"]["y"] > 240)) {
  162. this["diaY"]["reset"](Math["random"]() * 270, Math["random"]() * 240);
  163. if ((this["collected"] > 10) && (Math["random"]() > 0.56) && (!this["collect2"]["exists"])) {
  164. this["collect2"]["reset"](Math["random"]() * 270, Math["random"]() * 240);
  165. this["collected"]--;
  166. this["collect2"]["lifespan"] = 4000;
  167. };
  168. this["ball1"] = this["balls"]["create"](Math["random"]() * 270, Math["random"]() * 240, "avoid");
  169. this["ball1"]["body"]["velocity"]["setTo"](-60, -60);
  170. this["ball1"]["body"]["collideWorldBounds"] = true;
  171. this["ball1"]["body"]["bounce"]["setTo"](1, 1);
  172. this["collected"]++;
  173. this["score"] += 5;
  174. this["s"]["destroy"]();
  175. this["s"] = this["game"]["add"]["text"](0, 0, "分数: " + this["score"], this["textStyle"]);
  176. };
  177. if ((this["dragger"]["body"]["y"] < 240)) {
  178. this["diaY"]["reset"](Math["random"]() * 270, (Math["random"]() * 190) + 240);
  179. if ((this["collected"] > 10) && (Math["random"]() > 0.56) && (!this["collect2"]["exists"])) {
  180. this["collect2"]["reset"](Math["random"]() * 270, (Math["random"]() * 190) + 240);
  181. this["collected"]--;
  182. this["collect2"]["lifespan"] = 4000;
  183. };
  184. this["ball1"] = this["balls"]["create"](Math["random"]() * 270, (Math["random"]() * 190) + 240, "avoid");
  185. this["ball1"]["body"]["velocity"]["setTo"](60, 60);
  186. this["ball1"]["body"]["collideWorldBounds"] = true;
  187. this["ball1"]["body"]["bounce"]["setTo"](1, 1);
  188. this["collected"]++;
  189. this["score"] += 5;
  190. this["s"]["destroy"]();
  191. this["s"] = this["game"]["add"]["text"](0, 0, "分数: " + this["score"], this["textStyle"]);
  192. };
  193. },
  194. ballcollisionHandler: function() {
  195. this["collected"] = 0;
  196. // this.resetTouchPosition();
  197. game["state"]["start"]("lose");
  198. },
  199. collect2collision: function() {
  200. this["collect2"]["kill"]();
  201. this["ball1"]["destroy"]();
  202. this["score"] += 7;
  203. this["s"]["destroy"]();
  204. this["s"] = this["game"]["add"]["text"](0, 0, "分数: " + this["score"], this["textStyle"]);
  205. },
  206. collect2respawn: function() {
  207. if ((this["dragger"]["body"]["y"] > 240)) {
  208. this["collect2"]["reset"](Math["random"]() * 270, Math["random"]() * 240);
  209. };
  210. if ((this["dragger"]["body"]["y"] < 240)) {
  211. this["collect2"]["reset"](Math["random"]() * 270, (Math["random"]() * 190) + 240);
  212. };
  213. }
  214. };
  215. var lose_state = {
  216. preload: function() {
  217. this["game"]["stage"]["backgroundColor"] = "#0b70b7";
  218. },
  219. create: function() {
  220. // this["game"]["input"]["onTap"]["add"](this["tapped"], this);
  221. // this["game"]["input"]["moveCallback"] = null;
  222. this["game"]["input"]["touch"]["touchStartCallback"] = null;
  223. this["game"]["input"]["touch"]["touchMoveCallback"] = null;
  224. this["game"]["input"]["touch"]["touchEndCallback"] = null;
  225. this["gameover"] = this["game"]["add"]["sprite"](25, 50, "gameover");
  226. this["scoreText"] = this["game"]["add"]["text"](160-20, 140, "分数", {
  227. font: "bold 20px "+FONTFAMILY,
  228. fill: "white"
  229. });
  230. this["scoreText"] = this["game"]["add"]["text"](160-44/4*(main_state["score"].toString().length), 180, "" + main_state["score"], {
  231. font: "bold 44px "+FONTFAMILY,
  232. fill: "white"
  233. });
  234. highScore = main_state["score"] > highScore ? main_state["score"] : highScore;
  235. try{
  236. localStorage["avoiderHighScore"] = main_state["score"] > highScore ? main_state["score"] : highScore;
  237. }catch(e){
  238. }
  239. this["highScoreText"] = this["game"]["add"]["text"](100, 250, "历史最佳: " + highScore, {
  240. font: "bold 20px "+FONTFAMILY,
  241. fill: "white"
  242. });
  243. this["playagain"] = this["game"]["add"]["button"](4, 300, "playagain", function() {
  244. // game["state"]["start"]("start");
  245. window.location.reload();
  246. });
  247. this["playsharetips"] = this["game"]["add"]["button"](162, 300, "playsharetips", function() {
  248. dp_share();
  249. });
  250. this["getmoregame"] = this["game"]["add"]["button"](110, 400, "getmoregame", function() {
  251. clickMore();
  252. });
  253. this["doudouin"] = this["game"]["add"]["sprite"](0, 453, "doudouin");
  254. var shareScore = main_state["score"];
  255. dp_submitScore(shareScore);
  256. },
  257. update: function() {},
  258. tapped: function(e) {
  259. }
  260. };
  261. game["state"]["add"]("loading", loading_state);
  262. game["state"]["add"]("start", start_state);
  263. game["state"]["add"]("main", main_state);
  264. game["state"]["add"]("lose", lose_state);
  265. game["state"]["add"]("preload", preloader_state);
  266. game["state"]["start"]("preload");