{"id":287,"date":"2017-08-21T17:35:14","date_gmt":"2017-08-21T09:35:14","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=287"},"modified":"2021-10-19T10:45:46","modified_gmt":"2021-10-19T02:45:46","slug":"create-js%e5%bc%80%e5%8f%91%e7%ae%97%e6%9c%af%e5%a4%a7%e5%b8%88%e6%b8%b8%e6%88%8f","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=287","title":{"rendered":"Create.js\u5f00\u53d1\u7b97\u672f\u5927\u5e08\u6e38\u620f(\u7ec8\u7ed3\u7bc7)"},"content":{"rendered":"<p>\u8fd1\u671f\u6bd4\u8f83\u5fd9\uff0c\u535a\u5ba2\u4e5f\u5f88\u4e45\u6ca1\u66f4\u65b0\u4e86\uff0c\u8bb2\u771f\u5176\u5b9e\u6211\u7684\u535a\u5ba2\u6c34\u5e73\u6ca1\u90a3\u4e48\u9ad8\uff0c\u81ea\u5df1\u5199\u7684\u5c0f\u4e1c\u897f\u4e5f\u662f\u672c\u7740\u6015\u81ea\u5df1\u5fd8\u8bb0\uff0c\u4e5f\u7b97\u662f\u7ed9\u81ea\u5df1\u63d0\u9192\uff0c\u8bf6 \u6700\u8fd1\u8bb0\u5fc6\u91cc\u4e0d\u662f\u5f88\u597d\uff0c<\/p>\n<p>\u5148\u770bhtml\uff1a<\/p>\n<div>\n<div>&lt;canvas id=&#8221;game-canvas&#8221; width=&#8221;1280&#8243; height=&#8221;720&#8243;&gt;&lt;\/canvas&gt;<\/div>\n<\/div>\n<div>\u4e0ajs\u4ee3\u7801 \u4f9d\u65e7\u662f\u5165\u53e3\u6587\u4ef6\uff0c\u914d\u7f6e\u6587\u4ef6\uff0c\u9884\u52a0\u8f7d\u6587\u4ef6<\/div>\n<div>\n<div>\n<div>window.init = () =&gt; {<\/div>\n<div>letloadingValue=$(&#8220;#loading span&#8221;);<\/div>\n<div>window.queue=newc.LoadQueue();<\/div>\n<div>window.queue.installPlugin(createjs.Sound);<\/div>\n<div>window.queue.addEventListener(&#8220;complete&#8221;, (ev)=&gt;{<\/div>\n<div>window.game=newGame();<\/div>\n<div>$(&#8220;#loading&#8221;).hide();<\/div>\n<div>});<\/div>\n<div>window.queue.addEventListener(&#8220;progress&#8221;, (ev)=&gt;{<\/div>\n<div>loadingValue.text((ev.progress*100).toFixed() +&#8221;%&#8221;);<\/div>\n<div>})<\/div>\n<div>window.queue.loadManifest([<\/div>\n<div>{ id: &#8220;pointsound&#8221;, src: &#8220;music\/pointsound.mp3&#8221; },<\/div>\n<div>{ id: &#8220;noMuc&#8221;, src: &#8220;music\/noMuc.mp3&#8221; },<\/div>\n<div>{ id: &#8220;s2&#8221;, src: &#8220;music\/s2.mp3&#8221; },<\/div>\n<div>{ id: &#8220;nzb&#8221;, src: &#8220;music\/nzb.mp3&#8221; },<\/div>\n<div>])<\/div>\n<div>window.queue.loadManifest([<\/div>\n<div>{ id: &#8220;1&#8221;, src: &#8220;image\/assest\/1.png&#8221;},<\/div>\n<div>{ id: &#8220;2&#8221;, src: &#8220;image\/assest\/2.png&#8221;},<\/div>\n<div>{ id: &#8220;3&#8221;, src: &#8220;image\/assest\/3.png&#8221;},<\/div>\n<div>{ id: &#8220;4&#8221;, src: &#8220;image\/assest\/4.png&#8221;},<\/div>\n<div>{ id: &#8220;5&#8221;, src: &#8220;image\/assest\/5.png&#8221;},<\/div>\n<div>{ id: &#8220;6&#8221;, src: &#8220;image\/assest\/6.png&#8221;},<\/div>\n<div>{ id: &#8220;restart&#8221;, src: &#8220;image\/assest\/restart.png&#8221;},<\/div>\n<div>{ id: &#8220;scBtn&#8221;, src: &#8220;image\/direPic\/scBtn.png&#8221;},<\/div>\n<div>{ id: &#8220;sgBtn&#8221;, src: &#8220;image\/direPic\/sgBtn.png&#8221;},<\/div>\n<div>{ id: &#8220;pjBtn&#8221;, src: &#8220;image\/direPic\/middle.png&#8221;},<\/div>\n<div>{ id: &#8220;getScore&#8221;, src: &#8220;image\/assest\/getScore.png&#8221;},<\/div>\n<div>{ id: &#8220;gamebg&#8221;, src: &#8220;image\/assest\/gamebg.png&#8221;},<\/div>\n<div>{ id: &#8220;timer&#8221;, src: &#8220;image\/assest\/timer.png&#8221;}<\/div>\n<div>]);<\/div>\n<div>}<\/div>\n<div>\u8c8c\u4f3c\u6ca1\u5565\u53d8\u5316\uff0c\u8fd8\u662f\u8001\u4e00\u5957\uff0c<\/div>\n<\/div>\n<div>ok\u770b\u6e38\u620f\u903b\u8f91\u4ee3\u7801\u5904\u7406\uff0c<\/div>\n<div>\n<div>class Game {<\/div>\n<div>constructor(){<\/div>\n<div>this.stage=newc.Stage(&#8220;game-canvas&#8221;);<\/div>\n<div>c.Touch.enable(this.stage);<\/div>\n<div>this.stage.enableMouseOver();<\/div>\n<div>this.width=this.stage.canvas.width;<\/div>\n<div>this.height=this.stage.canvas.height;<\/div>\n<div>this.nTimeCount=300;<\/div>\n<div>this.gameDataSheet();<\/div>\n<div>this.render();<\/div>\n<div>createjs.Sound.play(&#8220;s2&#8221;, createjs.Sound.INTERRUPT_NONE,0,0,-1,1,0);<\/div>\n<div>}<\/div>\n<div>\u83b7\u53d6canvas\uff0c\u89e6\u5c4f\u4e8b\u4ef6\uff0c\u9f20\u6807\u76d1\u542c\u4e8b\u4ef6,\u821e\u53f0\u5bbd\uff0c\u821e\u53f0\u9ad8\uff0c\u5012\u8ba1\u65f6\uff0c\u5fc3\u8df3\u8ba1\u65f6\uff0cbgm\uff0c<\/div>\n<div>\n<div>\n<div>render(){<\/div>\n<div>this.stage.update();<\/div>\n<div>window.requestAnimationFrame(()=&gt;{<\/div>\n<div>this.render();<\/div>\n<div>})<\/div>\n<div>}<\/div>\n<div>gameDataSheet(){<\/div>\n<div>window.ceil=window.localStorage.vision;<\/div>\n<div>window.score=0;<\/div>\n<div>this.firstScreen();<\/div>\n<div>}<\/div>\n<div>firstScreen() {<\/div>\n<div>vargameView;<\/div>\n<div>this.gameView=newcreatejs.Container();<\/div>\n<div>this.stage.addChild(this.gameView);<\/div>\n<div>this.gamebg();<\/div>\n<div>this.bgMain(); \/\/\u6e38\u620f\u80cc\u666f<\/div>\n<div>this.Layout(); \/\/\u6e38\u620f\u5e03\u5c40\u6309\u94ae\uff0c\u6e38\u620f\u903b\u8f91<\/div>\n<div>this.fCountTime(); \/\/\u5012\u8ba1\u65f6\u6a21\u5757<\/div>\n<div>}<\/div>\n<\/div>\n<div>\n<div>killScore(){<\/div>\n<div>letgameView;<\/div>\n<div>this.gameView=newcreatejs.Container();<\/div>\n<div>this.stage.addChild(this.gameView);<\/div>\n<div>this.bgMain(); \/\/\u6e38\u620f\u80cc\u666f<\/div>\n<div>\/\/\u5f97\u5206\u9762\u677f<\/div>\n<div>varscorePanel=newcreatejs.Bitmap(window.queue.getResult(&#8220;getScore&#8221;));<\/div>\n<div>scorePanel.x=390;<\/div>\n<div>scorePanel.y=50;<\/div>\n<div>this.gameView.addChild(scorePanel);<\/div>\n<div>\/\/ \u5f97\u5206<\/div>\n<div>lettext_score=newcreatejs.Text(window.score,&#8221;38px Arial&#8221;, &#8220;#ffffff&#8221;);<\/div>\n<div>text_score.x=710;<\/div>\n<div>text_score.y=190;<\/div>\n<div>text_score.textAlign=&#8221;center&#8221;;<\/div>\n<div>this.gameView.addChild(text_score);<\/div>\n<div>\/\/ \u91cd\u65b0\u5f00\u59cb<\/div>\n<div>varrestart=newcreatejs.Bitmap(window.queue.getResult(&#8220;restart&#8221;));<\/div>\n<div>restart.x=560;<\/div>\n<div>restart.y=0;<\/div>\n<div>createjs.Tween.get(restart).to({ x:restart.x, y:restart.y+400 }, 700,createjs.Ease.backOut);<\/div>\n<div>this.gameView.addChild(restart);<\/div>\n<div>restart.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>window.location.href=&#8221;index.html&#8221;;<\/div>\n<div>})<\/div>\n<div>}<\/div>\n<div>\/\/ \u5012\u8ba1\u65f6<\/div>\n<div>fCountTime() {<\/div>\n<div>lettext_sum=newcreatejs.Text(this.nTimeCount, &#8220;20px Arial&#8221;, &#8220;#ffffff&#8221;);<\/div>\n<div>text_sum.x=1227;<\/div>\n<div>text_sum.y=9;<\/div>\n<div>text_sum.textAlign=&#8221;center&#8221; ;<\/div>\n<div>this.gameView.addChild(text_sum);<\/div>\n<div>window.upSteTimeOut=setTimeout(()=&gt;{<\/div>\n<div>if(this.nTimeCount&gt;0){<\/div>\n<div>this.nTimeCount&#8211;;<\/div>\n<div>text_sum.text=this.nTimeCount;<\/div>\n<div>this.gameView.removeChild(text_sum);<\/div>\n<div>this.fCountTime();<\/div>\n<div>}else{<\/div>\n<div>window.clearInterval(window.upSteTimeOut);<\/div>\n<div>this.reset();<\/div>\n<div>this.killScore();<\/div>\n<div>}<\/div>\n<div>},1000)<\/div>\n<div>}<\/div>\n<div>\/\/\u6e38\u620f\u5e03\u5c40\u5904\u7406<\/div>\n<div>Layout(){<\/div>\n<div>vartimer,getHero,insect,redImgHero,sgBtns,scBtns,pjBtns;<\/div>\n<div>varheroContainer=newcreatejs.Container();<\/div>\n<div>window.currentIndex=babyEye.randomRange(1, 7);<\/div>\n<div>getHero=window.queue.getResult(currentIndex);<\/div>\n<div>insect=newcreatejs.Bitmap(getHero);<\/div>\n<div>insect.scaleX=insect.scaleY=ceil;<\/div>\n<div>insect.regX=148;<\/div>\n<div>insect.regY=28;<\/div>\n<div>heroContainer.addChild(insect);<\/div>\n<div>heroContainer.x=this.width\/2;<\/div>\n<div>heroContainer.y=this.height\/6;<\/div>\n<div>this.gameView.addChild(heroContainer);<\/div>\n<div>\/\/3\u6309\u94ae<\/div>\n<div>sgBtns=newcreatejs.Bitmap(window.queue.getResult(&#8220;sgBtn&#8221;));<\/div>\n<div>sgBtns.x=850;<\/div>\n<div>sgBtns.y=300;<\/div>\n<div>this.gameView.addChild(sgBtns);<\/div>\n<div>\/\/1\u6309\u94ae<\/div>\n<div>scBtns=newcreatejs.Bitmap(window.queue.getResult(&#8220;scBtn&#8221;));<\/div>\n<div>scBtns.x=320;<\/div>\n<div>scBtns.y=320;<\/div>\n<div>this.gameView.addChild(scBtns);<\/div>\n<div>\/\/2\u6309\u94ae<\/div>\n<div>pjBtns=newcreatejs.Bitmap(window.queue.getResult(&#8220;pjBtn&#8221;));<\/div>\n<div>pjBtns.x=580;<\/div>\n<div>pjBtns.y=260;<\/div>\n<div>this.gameView.addChild(pjBtns);<\/div>\n<p>\/\/\u5012\u8ba1\u65f6\u56fe<\/p>\n<div>this.timer=newcreatejs.Bitmap(window.queue.getResult(&#8220;timer&#8221;));<\/div>\n<div>this.timer.x=1139;<\/div>\n<div>this.timer.y=4;<\/div>\n<div>this.gameView.addChild(this.timer);<\/div>\n<p>\u6e38\u620f\u6838\u5fc3\u4e3b\u903b\u8f91<\/p>\n<div>if(window.currentIndex==5||window.currentIndex==6){<\/div>\n<div>scBtns.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>this.gameView.removeChild(heroContainer);<\/div>\n<div>console.log(window.score);<\/div>\n<div>this.builderSuccess();<\/div>\n<div>})<\/div>\n<div>}else{<\/div>\n<div>scBtns.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>heroContainer.scaleX=heroContainer.scaleY+0.1;<\/div>\n<div>this.buildrFail();<\/div>\n<div>})<\/div>\n<div>}<\/div>\n<div>\/\/3\u5224\u65ad<\/div>\n<div>if(window.currentIndex==3||window.currentIndex==2){<\/div>\n<div>sgBtns.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>this.gameView.removeChild(heroContainer);<\/div>\n<div>this.builderSuccess();<\/div>\n<div>})<\/div>\n<div>}else{<\/div>\n<div>sgBtns.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>heroContainer.scaleX=heroContainer.scaleY+0.1;<\/div>\n<div>this.buildrFail();<\/div>\n<div>})<\/div>\n<div>}<\/div>\n<div>\/\/2\u5224\u65ad<\/div>\n<div>if(window.currentIndex==1||window.currentIndex==4){<\/div>\n<div>pjBtns.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>this.gameView.removeChild(heroContainer);<\/div>\n<div>this.builderSuccess();<\/div>\n<div>})<\/div>\n<div>}else{<\/div>\n<div>pjBtns.addEventListener(&#8220;mousedown&#8221;,(ev)=&gt;{<\/div>\n<div>heroContainer.scaleX=heroContainer.scaleY+0.1;<\/div>\n<div>this.buildrFail();<\/div>\n<div>})<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div>builderSuccess(){<\/div>\n<div>\/\/\u89c6\u6807\u7f29\u653e\u4ee3\u7801\u7b97\u6cd5<\/div>\n<div>if(ceil==0.10000000000000014){ \/\/ 0.20000000000000015<\/div>\n<div>ceil=ceil-0.3;<\/div>\n<div>ceil+=0.3;<\/div>\n<div>}else{<\/div>\n<div>ceil=ceil-0.1;<\/div>\n<div>}<\/div>\n<div>\/\/\u5206\u6570\u6210\u7ee9\u4ee3\u7801\u7b97\u6cd5<\/div>\n<div>if(window.score&lt;100){<\/div>\n<div>window.score=window.score+10;<\/div>\n<div>window.speed=window.speed+0.02;<\/div>\n<div>}else if(window.score == 100){<\/div>\n<div>window.score=100;<\/div>\n<div>}<\/div>\n<div>this.success(); \/\/\u4f60\u771f\u68d2<\/div>\n<div>window.clearInterval(window.upSteTimeOut); \/\/\u6e05\u9664\u5012\u8ba1\u65f6<\/div>\n<div>this.reset(); \/\/\u6e05\u9664\u753b\u5e03\u6240\u6709\u5185\u5bb9<\/div>\n<div>this.firstScreen(); \/\/\u753b\u5e03\u5237\u65b0<\/div>\n<div>}<\/div>\n<div>\u5f97\u5206\u903b\u8f91<\/div>\n<div>buildrFail(){<\/div>\n<div>window.speed=window.speed-0.1;<\/div>\n<div>this.fail();<\/div>\n<div>if(window.score&lt;=0){<\/div>\n<div>window.score=0;<\/div>\n<div>}else{<\/div>\n<div>window.score=window.score-10;}<\/div>\n<div>window.clearInterval(window.upSteTimeOut);<\/div>\n<div>this.reset();<\/div>\n<div>this.firstScreen();<\/div>\n<div>}<\/div>\n<div>\/\/\u6210\u529f\u5904\u7406<\/div>\n<div>success(){<\/div>\n<div>createjs.Sound.play(&#8220;nzb&#8221;, createjs.Sound.INTERRUPT_NONE, 0, 0, 0, 1, 0);<\/div>\n<div>$(&#8220;#sucImg&#8221;).show(500);<\/div>\n<div>$(&#8220;#sucImg&#8221;).hide(500);<\/div>\n<div>}<\/div>\n<div>\/\/\u5931\u8d25\u5904\u7406<\/div>\n<div>fail(){<\/div>\n<div>createjs.Sound.play(&#8220;noMuc&#8221;, createjs.Sound.INTERRUPT_NONE, 0, 0, 0, 1, 0);<\/div>\n<div>$(&#8220;#failImg&#8221;).show(500);<\/div>\n<div>$(&#8220;#failImg&#8221;).hide(500);<\/div>\n<div>}<\/div>\n<div>\/\/\u79fb\u9664\u753b\u5e03\u6240\u6709<\/div>\n<div>reset(){<\/div>\n<div>this.stage.removeAllChildren();<\/div>\n<div>this.stage.removeAllEventListeners();<\/div>\n<div>}<\/div>\n<div>\/\/\u64ad\u653e\u80cc\u666f<\/div>\n<div>gamebg(){<\/div>\n<div>letgamebg=window.queue.getResult(&#8220;gamebg&#8221;);<\/div>\n<div>letgamebgs=newcreatejs.Bitmap(gamebg);<\/div>\n<div>gamebgs.x=0;<\/div>\n<div>gamebgs.y=20;<\/div>\n<div>this.gameView.addChild(gamebgs);<\/div>\n<div>}<\/div>\n<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd1\u671f\u6bd4\u8f83\u5fd9\uff0c\u535a\u5ba2\u4e5f\u5f88\u4e45\u6ca1\u66f4\u65b0\u4e86\uff0c\u8bb2\u771f\u5176\u5b9e\u6211\u7684\u535a\u5ba2\u6c34\u5e73\u6ca1\u90a3\u4e48\u9ad8\uff0c\u81ea\u5df1\u5199\u7684\u5c0f\u4e1c\u897f\u4e5f\u662f\u672c\u7740\u6015\u81ea\u5df1\u5fd8\u8bb0\uff0c\u4e5f\u7b97\u662f\u7ed9\u81ea\u5df1\u63d0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":179,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/287"}],"collection":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":3,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":828,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/828"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/179"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}