main.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. (function(){
  2. window.onload = function()
  3. {
  4. game.init();
  5. };
  6. var game =
  7. {
  8. res: [
  9. {id:"begin", size:372, src:"images/0begin.png"},
  10. {id:"end", size:372, src:"images/1end.png"},
  11. {id:"help", size:372, src:"images/2help.png"},
  12. {id:"btns", size:77, src:"images/btns.png"},
  13. {id:"endbtns", size:151, src:"images/endbtns.png"},
  14. {id:"helpbtns", size:151, src:"images/helpbtns.png"},
  15. {id:"yxgzbtns", size:151, src:"images/yxgzbtns.png"},
  16. {id:"monkey", size:186, src:"images/monkey.png"},
  17. {id:"peach", size:151, src:"images/peach.png"},
  18. {id:"island", size:372, src:"images/island.png"},
  19. {id:"num1", size:15, src:"images/num1.png"},
  20. {id:"num2", size:29, src:"images/num2.png"}
  21. ],
  22. container: null,
  23. width: 0,
  24. height: 0,
  25. params: null,
  26. frames: 0,
  27. fps: 40,
  28. timer: null,
  29. eventTarget: null,
  30. state: null,
  31. monkey: null,
  32. peachs: [],
  33. maxPeachs: 5,
  34. collidedPeach: null,
  35. time: {total:59, current:59}, //TODO
  36. score: 0,
  37. scoreNum: null
  38. };
  39. var STATE =
  40. {
  41. MENU: 0,
  42. MAIN: 1,
  43. OVER: 2
  44. };
  45. var ns = window.game = game;
  46. game.init = function()
  47. {
  48. //加载进度信息
  49. var container = Q.getDOM("container");
  50. var div = document.createElement("div");
  51. div.style.position = "absolute";
  52. div.style.width = container.clientWidth + "px";
  53. div.style.left = "0px";
  54. div.style.top = (container.clientHeight >> 1) + "px";
  55. div.style.textAlign = "center";
  56. div.style.color = "#fff";
  57. div.style.font = Q.isMobile ? 'bold 16px 黑体' : 'bold 16px 宋体';
  58. div.style.textShadow = Q.isAndroid ? "0 2px 2px #111" : "0 2px 2px #ccc";
  59. container.appendChild(div);
  60. this.loader = div;
  61. //隐藏浏览器顶部导航
  62. setTimeout(game.hideNavBar, 10);
  63. if(Q.supportOrient)
  64. {
  65. window.onorientationchange = function(e)
  66. {
  67. game.hideNavBar();
  68. game.calcStagePosition();
  69. };
  70. }
  71. //加载图片素材
  72. var loader = new Q.ImageLoader();
  73. loader.addEventListener("loaded", Q.delegate(this.onLoadLoaded, this));
  74. loader.addEventListener("complete", Q.delegate(this.onLoadComplete, this));
  75. loader.load(this.res);
  76. };
  77. //加载进度条
  78. game.onLoadLoaded = function(e)
  79. {
  80. this.loader.innerHTML = "正在加载资源中,请稍候...<br>";
  81. this.loader.innerHTML += "(" + Math.round(e.target.getLoadedSize()/e.target.getTotalSize()*100) + "%)";
  82. }
  83. //加载完成
  84. game.onLoadComplete = function(e)
  85. {
  86. e.target.removeAllEventListeners();
  87. Q.getDOM("container").removeChild(this.loader);
  88. this.loader = null;
  89. this.images = e.images;
  90. //初始化一些类
  91. //console.log(this.stage);
  92. ns.Num.init();
  93. //启动游戏
  94. this.startup();
  95. }
  96. //获取图片资源
  97. game.getImage = function(id)
  98. {
  99. return this.images[id].image;
  100. }
  101. //启动游戏
  102. game.startup = function()
  103. {
  104. //手持设备的特殊webkit设置
  105. if(Q.isWebKit && Q.supportTouch)
  106. {
  107. document.body.style.webkitTouchCallout = "none";
  108. document.body.style.webkitUserSelect = "none";
  109. document.body.style.webkitTextSizeAdjust = "none";
  110. document.body.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
  111. }
  112. //初始化容器设置
  113. var colors = ["#00c2eb", "#cbfeff"];
  114. this.container = Q.getDOM("container");
  115. this.container.style.overflow = "hidden";
  116. this.container.style.background = "-moz-linear-gradient(top, "+ colors[0] +", "+ colors[1] +")";
  117. this.container.style.background = "-webkit-gradient(linear, 0 0, 0 bottom, from("+ colors[0] +"), to("+ colors[1] +"))";
  118. this.container.style.background = "-o-linear-gradient(top, "+ colors[0] +", "+ colors[1] +")";
  119. this.container.style.filter = "progid:DXImageTransform.Microsoft.gradient(startColorstr="+ colors[0] +", endColorstr="+ colors[1] +")";
  120. this.width = this.container.clientWidth;
  121. this.height = this.container.clientHeight;
  122. //获取URL参数设置
  123. this.params = Q.getUrlParams();
  124. // this.maxPeachs = this.params.peachs || 20;
  125. // this.time = this.params.time ? {total:this.params.time, current:this.params.time} : {total:15, current:15};
  126. // this.fps = this.params.fps || 40;
  127. //初始化context
  128. var context = null;
  129. if(this.params.canvas)
  130. {
  131. var canvas = Q.createDOM("canvas", {id:"canvas", width:this.width, height:this.height, style:{position:"absolute"}});
  132. this.container.appendChild(canvas);
  133. this.context = new Q.CanvasContext({canvas:canvas});
  134. }else
  135. {
  136. this.context = new Q.DOMContext({canvas:this.container});
  137. }
  138. //创建舞台
  139. this.stage = new Q.Stage({width:this.width, height:this.height, context:this.context, update:Q.delegate(this.update, this)});
  140. ns.Peach.init(this.stage.height);
  141. //初始化定时器
  142. var timer = new Q.Timer(1000 / this.fps);
  143. timer.addListener(this.stage);
  144. timer.addListener(Q.Tween);
  145. timer.start();
  146. this.timer = timer;
  147. //预加载背景音乐
  148. // var audio = new Quark.Audio("images/a.mp3", true, true, true);
  149. // this.audio = audio;
  150. //注册事件
  151. var me = this;
  152. var em = new Q.EventManager();
  153. var events = Q.supportTouch ? ["touchstart", "touchmove", "touchend"] : ["mousedown", "mousemove", "mouseup"];
  154. em.register(this.context.canvas, events, function(e)
  155. {
  156. var ne = (e.touches && e.touches.length > 0) ? e.touches[0] :
  157. (e.changedTouches && e.changedTouches.length > 0) ? e.changedTouches[0] : e;
  158. //确保touchend事件的类型正确
  159. if(Q.supportTouch) ne.type = e.type;
  160. var x = ne.pageX - me.stage.stageX, y = ne.pageY - me.stage.stageY;
  161. var obj = me.stage.getObjectUnderPoint(x, y);
  162. //加载音效
  163. /* if(me.audio && !me.audio.loading)
  164. {
  165. me.audio.loading = true;
  166. me.audio.load();
  167. }
  168. */
  169. if(me.eventTarget != null && me.eventTarget != obj)
  170. {
  171. if(me.eventTarget.onEvent != null) me.eventTarget.onEvent({type:"mouseout"});
  172. me.eventTarget = null;
  173. }
  174. if(obj != null)
  175. {
  176. me.eventTarget = obj;
  177. if(obj.useHandCursor) me.context.canvas.style.cursor = "pointer";
  178. if(obj.onEvent != null) obj.onEvent(ne);
  179. }
  180. if(me.state == STATE.MAIN)
  181. {
  182. if (ne.type == "touchstart" && obj.id == "monkey"){
  183. me.monkey.mov = true;
  184. }
  185. if(ne.type == "touchmove")
  186. {
  187. if (me.monkey.mov){
  188. me.monkey.x = ne.pageX - me.monkey.getCurrentWidth()/2;
  189. }
  190. }
  191. if (ne.type == "touchend"){
  192. me.monkey.mov = false;
  193. game.audioobj.play();
  194. }
  195. }else if(me.state == STATE.OVER && ne.type != "mousemove" && ne.type != "touchmove")
  196. {
  197. //me.restart();
  198. }
  199. }, true, true);
  200. //按键事件
  201. em.register(document, ["keydown", "keyup"], function(e)
  202. {
  203. var key = e.keyCode;
  204. if(me.state != STATE.MAIN) return;
  205. if(key == Q.KEY.A || key == Q.KEY.LEFT)
  206. {
  207. if(e.type == "keydown") me.monkey.move(-1);
  208. else if(e.type == "keyup") me.monkey.stopMove();
  209. }else if(key == Q.KEY.D || key == Q.KEY.RIGHT)
  210. {
  211. if(e.type == "keydown") me.monkey.move(1);
  212. else if(e.type == "keyup") me.monkey.stopMove();
  213. }
  214. }, false, false);
  215. //显示开始菜单
  216. this.showMenu();
  217. var audioobj=$("audio").get(0);
  218. this.audioobj = audioobj;
  219. setInterval(function(){game.audioobj.play();}, 3000);
  220. };
  221. //显示开始菜单
  222. game.showMenu = function()
  223. {
  224. if(this.begin == null)
  225. {
  226. //启动画面
  227. var begin = new Q.Bitmap({id:"begin", image:this.getImage("begin")});
  228. var sX = this.stage.width/begin.width;
  229. var sY = this.stage.height/begin.height;
  230. begin.scaleX = sX;
  231. begin.scaleY = sY;
  232. begin.x = 0;
  233. begin.y = 0;
  234. this.begin = begin;
  235. //TODOBEGIN
  236. //开始按钮
  237. var playBtn = new Q.Button({id:"playBtn", image:this.getImage("btns")});
  238. playBtn.setUpState({rect:[0,286,200,200]});
  239. playBtn.setOverState({rect:[0,86,200,200]});
  240. playBtn.scaleX = sX;
  241. playBtn.scaleY = sY;
  242. playBtn.regX = playBtn.width >> 1;
  243. playBtn.regY = playBtn.height >> 1;
  244. playBtn.x = this.width * 0.5;
  245. playBtn.y = this.height * 0.7;
  246. this.playBtn = playBtn;
  247. playBtn.onEvent = function(e)
  248. {
  249. Q.Button.prototype.onEvent.call(this, e);
  250. if(e.type == "mouseup" || e.type == "touchend")
  251. {
  252. game.stage.removeAllChildren();
  253. game.context.canvas.style.cursor = "";
  254. if(game.state == STATE.MENU)
  255. {
  256. trace("game start");
  257. setTimeout(Q.delegate(game.showMain, game), 100);
  258. }else if(game.state == STATE.OVER)
  259. {
  260. trace("game restart");
  261. game.overlay.parentNode.removeChild(game.overlay);
  262. game.stage.removeAllChildren();
  263. game.score = 0;
  264. game.time.current = game.time.total;
  265. game.timer.paused = false;
  266. setTimeout(Q.delegate(game.showMain, game), 100);
  267. }
  268. }else if(e.type == "mouseout")
  269. {
  270. game.context.canvas.style.cursor = "";
  271. }
  272. }
  273. //帮助提示
  274. var tip = new Q.Button({id:"yxgzBtn", image:this.getImage("yxgzbtns")});
  275. tip.setUpState({rect:[0,0,312,41]});
  276. tip.scaleX = sX*0.7;
  277. tip.scaleY = sY*0.7;
  278. tip.x = 200*sX;
  279. tip.y = 900*sY;
  280. tip.onEvent = function(e)
  281. {
  282. Q.Button.prototype.onEvent.call(this, e);
  283. if(e.type == "mouseup" || e.type == "touchend")
  284. {
  285. game.showHelp();
  286. }else if(e.type == "mouseout")
  287. {
  288. game.context.canvas.style.cursor = "";
  289. }
  290. }
  291. this.tip = tip;
  292. }
  293. this.state = STATE.MENU;
  294. this.stage.addChild(this.begin, this.playBtn, this.tip);
  295. }
  296. //游戏主场景
  297. game.showMain = function()
  298. {
  299. var me = this;
  300. //设置当前状态
  301. this.state = STATE.MAIN;
  302. if(this.tip.parentNode) this.tip.parentNode.removeChild(this.tip);
  303. //启动重力感应
  304. //Q.Orientation.register(function(data){game.acceleration = data;});
  305. if(this.island == null)
  306. {
  307. //海岛
  308. var island = new Q.Bitmap({id:"island", image:this.getImage("island")});
  309. island.scaleX = this.stage.width/island.width;
  310. island.scaleY = this.stage.height/island.height;
  311. island.x = 0;
  312. island.y = 0;
  313. this.island = island;
  314. //创建猴子
  315. var monkey = new ns.Monkey({id:"monkey"});
  316. monkey.scaleX = monkey.scaleY = island.scaleX*0.8;
  317. this.monkey = monkey;
  318. //创建下落的球组
  319. this.createPeachs();
  320. }
  321. //初始化
  322. this.monkey.x = this.width - this.monkey.getCurrentWidth() >> 1;
  323. this.monkey.y = this.height - this.monkey.getCurrentHeight() - 5;
  324. this.monkey.dirX = 0;
  325. this.monkey.dirY = 0;
  326. this.monkey.jumping = false;
  327. this.monkey.avatar.gotoAndPlay("idle");
  328. //添加所有对象到舞台
  329. this.stage.addChild(this.island);
  330. for(var i = 0; i < this.peachs.length; i++)
  331. {
  332. var peach = this.peachs[i];
  333. peach.reset(ns.Peach.getRandomType(this.time.current));
  334. this.stage.addChild(peach);
  335. }
  336. this.stage.addChild(this.monkey);
  337. //显示倒计时
  338. this.showTimer();
  339. //显示得分
  340. this.updateScore();
  341. }
  342. //创建小球
  343. game.createPeachs = function()
  344. {
  345. var minX = 100, maxX = this.width-100, minY = -500, maxY = 0;
  346. //for(var i = 0; i < 1; i++)
  347. for(var i = 0; i < this.maxPeachs; i++)
  348. {
  349. var peach = new ns.Peach({id:"peach"+i, type:ns.Peach.getRandomType(this.time.current)});
  350. peach.scaleX = peach.scaleY = this.stage.width*0.8/this.island.width;
  351. this.peachs.push(peach);
  352. }
  353. }
  354. //主更新方法
  355. game.update = function(timeInfo)
  356. {
  357. this.frames++;
  358. if(this.state == STATE.MENU)
  359. {
  360. }else if(this.state == STATE.MAIN)
  361. {
  362. this.updatePeachs();
  363. this.updateMonkey();
  364. }
  365. }
  366. //更新小球
  367. game.updatePeachs = function()
  368. {
  369. var me = this, peachs = this.peachs, minBottom = 80;
  370. for(var i = 0; i < peachs.length; i++)
  371. {
  372. var peach = me.peachs[i];
  373. if(peach.delay > 0)
  374. {
  375. peach.delay -= 1;
  376. continue;
  377. }
  378. if(peach.currentSpeedY > 0) peach.currentSpeedY += 0.05;
  379. else if(peach.currentSpeedY < 0) peach.currentSpeedY += 0.15;
  380. peach.y += peach.currentSpeedY;
  381. peach.x += peach.currentSpeedX;
  382. if(peach.y > me.height - minBottom && peach.alpha > 0)
  383. {
  384. peach.alpha -= 0.1;
  385. peach.fading = true;
  386. }
  387. if(peach.y > me.height)
  388. {
  389. peach.reset(ns.Peach.getRandomType(this.time.current));
  390. }
  391. }
  392. }
  393. //更新猴子位置
  394. game.updateMonkey = function()
  395. {
  396. var acc = this.acceleration, dw = this.monkey.getCurrentWidth(), dh = this.monkey.getCurrentHeight();
  397. if(acc != null)
  398. {
  399. //重力感应移动
  400. var ax = acc.accelerationX, ay = acc.accelerationY, or = window.orientation;
  401. var av = (or%180) ? ay : ax;
  402. var dv = (or%180) ? (ax<0?1:-1) : (ay<0?-1:1);
  403. this.monkey.currentSpeedX = this.monkey.jumping ? 0.5*Math.abs(av) : this.monkey.currentSpeedX + 0.08*Math.abs(av);
  404. if(av*dv > 0.5)
  405. {
  406. this.monkey.x -= this.monkey.currentSpeedX*1;
  407. if(this.monkey.x < 0) this.monkey.x = 0;
  408. }else if(av*dv < -0.5)
  409. {
  410. this.monkey.x += this.monkey.currentSpeedX*1;
  411. if(this.monkey.x > this.width - dw) this.monkey.x = this.width - dw;
  412. }else
  413. {
  414. this.monkey.currentSpeedX = this.monkey.speedX;
  415. }
  416. }else if(this.monkey.dirX != 0)
  417. {
  418. //普通移动
  419. //this.monkey.currentSpeedX += 0.1;
  420. this.monkey.x += this.monkey.currentSpeedX * this.monkey.dirX;
  421. if(this.monkey.x < 0) this.monkey.x = 0;
  422. else if(this.monkey.x > this.width - dw) this.monkey.x = this.width - dw;
  423. }
  424. this.checkCollision()
  425. }
  426. var sortPeachFunc = function(a, b){return a.y < b.y;}
  427. //海豚与球的碰撞检测
  428. game.checkCollision = function()
  429. {
  430. var me = this, peachs = this.peachs, monkey = this.monkey;
  431. //根据球的Y轴排序
  432. peachs.sort(sortPeachFunc);
  433. for(var i = 0; i < peachs.length; i++)
  434. {
  435. var peach = peachs[i];
  436. if(peach.fading) continue;
  437. var gapH = gapV = 0//peach.getCurrentHeight()*0.5; peach.getCurrentWidth()*0.5,
  438. var dx = peach.x - monkey.x, dy = monkey.y - peach.y;
  439. //trace(peach, monkey.y, peach.y, gapV, peach.x, monkey.x, gapH);
  440. if(dx <= monkey.getCurrentWidth()+gapH && dx >= 0 && dy <= gapV && dy >= -gapV-100)
  441. {
  442. this.addScore(peach, peach.currentScore);
  443. peach.y += 1000;
  444. return true;
  445. }
  446. }
  447. return false;
  448. }
  449. //得分
  450. game.addScore = function(peach, score)
  451. {
  452. //if(this.addNum == null)
  453. //{
  454. var container = new Q.DisplayObjectContainer({id:"addNum", width:100, height:65});
  455. var plus = new ns.Num({id:"plus", type:ns.Num.Type.num1});
  456. if (score>=0){
  457. plus.setValue(11);
  458. }else{
  459. plus.setValue(10);
  460. }
  461. container.addChild(plus);
  462. var num = new ns.Num({id:"num", type:ns.Num.Type.num1});
  463. num.x = plus.x + plus.width - 15;
  464. num.setValue(Math.abs(score))
  465. container.addChild(num);
  466. // this.addNum = container;
  467. //}
  468. container.x = peach.x - 50;
  469. container.y = peach.y - 100;
  470. container.scaleX = container.scaleY = this.island.scaleY*2;
  471. this.stage.addChild(container);
  472. container.alpha = 1;
  473. this.score += score;
  474. // console.log(this.score);
  475. if (this.score <= 0) {this.score = 0}
  476. if (this.score >= 59) {this.score = 59}
  477. this.updateScore();
  478. Q.Tween.to(container, {y:container.y-100, alpha:0}, {time:1000});
  479. }
  480. //更新总得分
  481. game.updateScore = function()
  482. {
  483. if(this.scoreNum == null)
  484. {
  485. var container = new Q.DisplayObjectContainer({id:'score', width:75, height:65});
  486. // var num0 = new ns.Num({id:"num0", type:ns.Num.Type.num2});
  487. // var num1 = new ns.Num({id:"num1", type:ns.Num.Type.num2});
  488. var num$ = new ns.Num({id:"num$", type:ns.Num.Type.num2});
  489. var num2 = new ns.Num({id:"num2", type:ns.Num.Type.num2});
  490. var num3 = new ns.Num({id:"num3", type:ns.Num.Type.num2});
  491. // num$.x = 25;
  492. num$.setValue(11);
  493. num2.x = 25;
  494. num3.x = 50;
  495. container.addChild(num$, num2, num3);
  496. container.scaleX = container.scaleY = this.island.scaleY*1.5;
  497. container.x = this.width - container.getCurrentWidth() - 15 >> 0;
  498. //container.y = this.stage.height - 50;
  499. container.y = 15;
  500. this.scoreNum = container;
  501. }
  502. var str = this.score.toString(), len = str.length;
  503. str = len > 2 ? str.slice(len - 2) : str;
  504. while(str.length < 2) str = "0" + str;
  505. this.scoreNum.getChildAt(1).setValue(Number(str[0]));
  506. this.scoreNum.getChildAt(2).setValue(Number(str[1]));
  507. this.stage.addChild(this.scoreNum);
  508. //console.log(this.scoreNum);
  509. }
  510. //显示倒计时
  511. game.showTimer = function()
  512. {
  513. if(this.countdown == null)
  514. {
  515. //初始化倒计时
  516. var countdown = new Q.DisplayObjectContainer({id:'countdown', width:250, height:65});
  517. var num1 = new ns.Num({id:"min1", type:ns.Num.Type.num2});
  518. var num2 = new ns.Num({id:"min2", type:ns.Num.Type.num2});
  519. var sep = new ns.Num({id:"sep", type:ns.Num.Type.num2});
  520. var sec1 = new ns.Num({id:"sec1", type:ns.Num.Type.num2});
  521. var sec2 = new ns.Num({id:"sec2", type:ns.Num.Type.num2});
  522. num2.x = 45;
  523. sep.x = 80;
  524. sec1.x = 125;
  525. sec2.x = 170;
  526. sep.setValue(10);
  527. countdown.addChild(num1, num2, sep, sec1, sec2);
  528. countdown.scaleX = countdown.scaleY = this.island.scaleY*1.5;
  529. countdown.x = 20;
  530. countdown.y = 15;
  531. this.countdown = countdown;
  532. }
  533. this.stage.addChild(this.countdown);
  534. this.time.current = this.time.total;
  535. this.updateTimer();
  536. //启动倒计时Tween
  537. Q.Tween.to(this.time, null, {time:1000, loop:true,
  538. onComplete:function(tween)
  539. {
  540. game.updateTimer();
  541. if(game.time.current <= -1)
  542. {
  543. tween.stop();
  544. game.gameOver();
  545. }
  546. }});
  547. }
  548. //更新倒计时数值
  549. game.updateTimer = function()
  550. {
  551. var me = this, time = this.time;
  552. var min = Math.floor(time.current / 60), sec = time.current % 60;
  553. me.countdown.getChildAt(0).setValue(min>=10?Math.floor(min/10) : 0);
  554. me.countdown.getChildAt(1).setValue(min>=10?(min%10) : min);
  555. me.countdown.getChildAt(3).setValue(sec>=10?Math.floor(sec/10) : 0);
  556. me.countdown.getChildAt(4).setValue(sec>=10?(sec%10) : sec);
  557. time.current--;
  558. }
  559. //游戏结束
  560. game.gameOver = function()
  561. {
  562. trace("game over:", this.score);
  563. this.timer.pause();
  564. if(this.context.context == null)
  565. {
  566. if(this.overlay == null)
  567. {
  568. this.overlay = Q.createDOM("div", {id:"overlay", style:
  569. {
  570. position: "absolute",
  571. width: this.width + "px",
  572. height: this.height + "px",
  573. background: "#000",
  574. opacity: 0.4
  575. }});
  576. }
  577. this.container.lastChild.appendChild(this.overlay);
  578. }
  579. this.state = STATE.OVER;
  580. this.playBtn.setState(Q.Button.state.OVER);
  581. game.showEnd();
  582. this.stage.step();
  583. //保存分数
  584. this.saveScore(this.score);
  585. }
  586. //重新开始
  587. game.restart = function()
  588. {
  589. trace("game restart");
  590. this.overlay.parentNode.removeChild(this.overlay);
  591. this.stage.removeAllChildren();
  592. this.timer.paused = false;
  593. this.showMenu();
  594. this.score = 0;
  595. this.time.current = this.time.total;
  596. }
  597. //获取保存的分数
  598. game.getScore = function()
  599. {
  600. var key = "monkey_score";
  601. if(Q.supportStorage && localStorage.hasOwnProperty(key))
  602. {
  603. var score = Number(localStorage.getItem("monkey_score"));
  604. return score;
  605. }
  606. return 0;
  607. }
  608. //保存分数到localStorage
  609. game.saveScore = function(score)
  610. {
  611. var key = "monkey_score";
  612. if(Q.supportStorage)
  613. {
  614. localStorage.removeItem(key);
  615. localStorage.setItem(key, score);
  616. }
  617. }
  618. //显示结束页面
  619. game.showEnd = function(){
  620. //TODOEND
  621. //结束画面
  622. var end = new Q.Bitmap({id:"end", image:this.getImage("end")});
  623. var sY = end.scaleY = this.stage.height/end.height;
  624. var sX = end.scaleX = this.stage.width/end.width;
  625. end.x = 0;
  626. end.y = 0;
  627. var getPriceBtn = new Q.Button({id:"gpBtn", image:this.getImage("endbtns")});
  628. getPriceBtn.setUpState({rect:[0,0,337,125]});
  629. getPriceBtn.scaleX = sX;
  630. getPriceBtn.scaleY = sY;
  631. getPriceBtn.x = 175*sX;
  632. getPriceBtn.y = 755*sY;
  633. getPriceBtn.onEvent = function(e)
  634. {
  635. Q.Button.prototype.onEvent.call(this, e);
  636. if(e.type == "mouseup" || e.type == "touchend")
  637. {
  638. //TODO
  639. }else if(e.type == "mouseout")
  640. {
  641. game.context.canvas.style.cursor = "";
  642. }
  643. }
  644. var rePlayBtn = new Q.Button({id:"rpBtn", image:this.getImage("endbtns")});
  645. rePlayBtn.setUpState({rect:[0,176,337,125]});
  646. rePlayBtn.scaleX = sX;
  647. rePlayBtn.scaleY = sY;
  648. rePlayBtn.x = 175*sX;
  649. rePlayBtn.y = 585*sY;
  650. rePlayBtn.onEvent = function(e)
  651. {
  652. Q.Button.prototype.onEvent.call(this, e);
  653. if(e.type == "mouseup" || e.type == "touchend")
  654. {
  655. game.restart();
  656. }else if(e.type == "mouseout")
  657. {
  658. game.context.canvas.style.cursor = "";
  659. }
  660. }
  661. var end_num = this.score;
  662. var end_num_str = end_num.toString(), len = end_num_str.length;
  663. end_num_str = len > 2 ? end_num_str.slice(len - 2) : end_num_str;
  664. while(end_num_str.length < 2) end_num_str = "0" + end_num_str;
  665. var container = new Q.DisplayObjectContainer({id:'end_score', width:75, height:65});
  666. var end_num$ = new ns.Num({id:"end_num$", type:ns.Num.Type.num2});
  667. var end_num2 = new ns.Num({id:"end_num2", type:ns.Num.Type.num2});
  668. var end_num3 = new ns.Num({id:"end_num3", type:ns.Num.Type.num2});
  669. end_num$.setValue(11);
  670. end_num2.setValue(Number(end_num_str[0]));
  671. end_num3.setValue(Number(end_num_str[1]));
  672. end_num2.x = 25;
  673. end_num3.x = 50;
  674. container.addChild(end_num$, end_num2, end_num3);
  675. container.scaleX = sX*1.3;
  676. container.scaleY = sY*1.3;
  677. container.x = 380 * sX;
  678. container.y = 444 * sY;
  679. this.endNum = container;
  680. this.end = end;
  681. this.getPriceBtn = getPriceBtn;
  682. this.rePlayBtn = rePlayBtn;
  683. this.stage.addChild(this.end, this.getPriceBtn, this.rePlayBtn, this.endNum);
  684. }
  685. game.showHelp = function(){
  686. //TODOHELP
  687. if(this.tip.parentNode) this.tip.parentNode.removeChild(this.tip);
  688. var help = new Q.Bitmap({id:"help", image:this.getImage("help")});
  689. var sY = help.scaleY = this.stage.height/help.height;
  690. var sX = help.scaleX = this.stage.width/help.width;
  691. help.x = 0;
  692. help.y = 0;
  693. var rePlayBtn = new Q.Button({id:"hpBtn", image:this.getImage("helpbtns")});
  694. rePlayBtn.setUpState({rect:[0,0,334,121]});
  695. rePlayBtn.scaleX = sX;
  696. rePlayBtn.scaleY = sY;
  697. rePlayBtn.x = 160*sX;
  698. rePlayBtn.y = 774*sY;
  699. rePlayBtn.onEvent = function(e)
  700. {
  701. Q.Button.prototype.onEvent.call(this, e);
  702. if(e.type == "mouseup" || e.type == "touchend")
  703. {
  704. trace("game restart");
  705. game.stage.removeAllChildren();
  706. game.showMenu();
  707. }else if(e.type == "mouseout")
  708. {
  709. game.context.canvas.style.cursor = "";
  710. }
  711. }
  712. this.help = help;
  713. this.rePlayBtn = rePlayBtn;
  714. this.stage.addChild(this.help, this.rePlayBtn);
  715. }
  716. //显示当前FPS值
  717. game.showFPS = function()
  718. {
  719. var me = this, fpsContainer = Quark.getDOM("fps");
  720. setInterval(function()
  721. {
  722. fpsContainer.innerHTML = "FPS:" + me.frames;
  723. me.frames = 0;
  724. }, 1000);
  725. }
  726. //隐藏浏览器顶部导航
  727. game.hideNavBar = function()
  728. {
  729. window.scrollTo(0, 1);
  730. }
  731. //重新计算舞台stage在页面中的偏移
  732. game.calcStagePosition = function()
  733. {
  734. if(game.stage)
  735. {
  736. var offset = Q.getElementOffset(game.stage.context.canvas);
  737. game.stage.stageX = offset.left;
  738. game.stage.stageY = offset.top;
  739. }
  740. }
  741. })();