main.js 22 KB

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