{"id":653,"date":"2019-12-07T14:04:05","date_gmt":"2019-12-07T06:04:05","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=653"},"modified":"2019-12-07T14:04:05","modified_gmt":"2019-12-07T06:04:05","slug":"egret-%e6%9e%81%e9%99%90%e9%a3%9e%e8%bd%a6%e6%b8%b8%e6%88%8f%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=653","title":{"rendered":"egret-\u6781\u9650\u98de\u8f66\u6e38\u620f\u5f00\u53d1"},"content":{"rendered":"<p>\u5148\u521b\u5efa\u4e00\u4e2aegret\u6e38\u620f\u9879\u76ee \uff0c<\/p>\n<p>\u7136\u540e\u5728\u6211\u4eec\u7684main.ts\u6587\u4ef6\u5199\u4e0a\u4ee5\u4e0b\u4ee3\u7801<br \/>\n<code><br \/>\nclass Main extends egret.DisplayObjectContainer {<br \/>\npublic constructor() {<br \/>\nsuper();<br \/>\nthis.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);<br \/>\n}<br \/>\nprivate onAddToStage(event: egret.Event) {<\/code><\/p>\n<p>egret.lifecycle.addLifecycleListener((context) =&gt; {<br \/>\n\/\/ custom lifecycle plugin<\/p>\n<p>context.onUpdate = () =&gt; {<\/p>\n<p>}<br \/>\n})<\/p>\n<p>egret.lifecycle.onPause = () =&gt; {<br \/>\negret.ticker.pause();<br \/>\n}<\/p>\n<p>egret.lifecycle.onResume = () =&gt; {<br \/>\negret.ticker.resume();<br \/>\n}<\/p>\n<p>this.runGame().catch(e =&gt; {<br \/>\nconsole.log(e);<br \/>\n})<\/p>\n<p>}<\/p>\n<p>private async runGame() {<br \/>\nawait this.loadResource()<br \/>\nthis.createGameScene();<br \/>\nconst result = await RES.getResAsync(&#8220;description_json&#8221;)<br \/>\n\/\/ this.startAnimation(result);<br \/>\nawait platform.login();<br \/>\nconst userInfo = await platform.getUserInfo();<br \/>\nconsole.log(userInfo);<\/p>\n<p>}<\/p>\n<p>private async loadResource() {<br \/>\ntry {<br \/>\nconst loadingView = new LoadingUI();<br \/>\nthis.stage.addChild(loadingView);<br \/>\nawait RES.loadConfig(&#8220;resource\/default.res.json&#8221;, &#8220;resource\/&#8221;);<br \/>\nawait RES.loadGroup(&#8220;preload&#8221;, 0, loadingView);<br \/>\nthis.stage.removeChild(loadingView);<br \/>\n}<br \/>\ncatch (e) {<br \/>\nconsole.error(e);<br \/>\n}<br \/>\n}<\/p>\n<p>private textfield: egret.TextField;<\/p>\n<p>private car: egret.Bitmap;<br \/>\nprivate isMove: boolean = false;<br \/>\nprivate radius: number<br \/>\nprivate top_cx: number;<br \/>\nprivate top_cy: number;<br \/>\nprivate bot_cx: number;<br \/>\nprivate bot_cy: number;<br \/>\nprivate left_x: number;<br \/>\nprivate right_x: number;<br \/>\nprivate speed: number = 10;<br \/>\nprivate cl: number = 0;<\/p>\n<p>\/**<br \/>\n* \u521b\u5efa\u6e38\u620f\u573a\u666f<br \/>\n* Create a game scene<br \/>\n*\/<br \/>\nprivate createGameScene() {<br \/>\nthis.createBgPic()<br \/>\nthis.createPaoDao()<\/p>\n<p>this.car = new egret.Bitmap();<br \/>\nthis.car.texture = RES.getRes(&#8216;car_png&#8217;);<br \/>\nthis.addChild(this.car)<\/p>\n<p>this.car.anchorOffsetX = this.car.width \/ 2;<br \/>\nthis.car.anchorOffsetY = this.car.width \/ 2;<br \/>\nthis.car.y = this.stage.stageHeight &#8211; this.stage.stageWidth \/ 2 &#8211; 10;<br \/>\nthis.car.x = this.stage.stageWidth &#8211; this.car.width \/ 2;<\/p>\n<p>this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onDrive, this)<br \/>\n}<br \/>\nprivate createBgPic() {<br \/>\nlet bgPic:egret.Bitmap = this.createBitmapByName(&#8216;bg_jpg&#8217;)<br \/>\nthis.addChild(bgPic);<br \/>\nbgPic.width = this.stage.stageWidth;<br \/>\nbgPic.height = this.stage.stageHeight;<br \/>\n}<br \/>\nprivate createPaoDao() {<br \/>\nlet top: egret.Shape = new egret.Shape()<br \/>\ntop.graphics.lineStyle(10, 0xff0304);<br \/>\ntop.graphics.beginFill(0xff0000, 0);<br \/>\n\/\/\u7ed8\u5236 \u5706\u5fc3\u7684\u4f4d\u7f6ex,\u5706\u5fc3\u7684\u4f4d\u7f6ey,\u534a\u5f84\uff0c\u4ece\u4ec0\u4e48\u89d2\u5ea6\u5230\u4ec0\u4e48\u89d2\u5ea6\u7ed8\u5236<br \/>\ntop.graphics.drawArc(this.stage.stageWidth \/ 2, this.stage.stageWidth \/ 2 &#8211; 10, this.stage.stageWidth \/ 2 &#8211; 40, -Math.PI, 0, false)<br \/>\ntop.graphics.endFill();<br \/>\nthis.addChild(top);<\/p>\n<p>this.radius = this.stage.stageWidth \/ 2 &#8211; 40;<br \/>\nthis.top_cx = this.stage.stageWidth \/ 2;<br \/>\nthis.top_cy = this.stage.stageWidth \/ 2 &#8211; 10;<\/p>\n<p>let left: egret.Shape = new egret.Shape();<br \/>\nleft.graphics.lineStyle(10, 0xff0000);<br \/>\n\/\/\u4ece\u4ec0\u4e48\u4f4d\u7f6e\u5f00\u59cb<br \/>\nleft.graphics.moveTo(40, this.stage.stageWidth \/ 2 &#8211; 10);<br \/>\n\/\/\u5230\u4ec0\u4e48\u4f4d\u7f6e\u7ed3\u675f<br \/>\nleft.graphics.lineTo(40, this.stage.stageHeight &#8211; this.stage.stageWidth \/ 2 &#8211; 10);<br \/>\nleft.graphics.endFill();<br \/>\nthis.addChild(left);<br \/>\nthis.left_x = 40;<\/p>\n<p>let right: egret.Shape = new egret.Shape();<br \/>\nright.graphics.lineStyle(10, 0xff0000);<br \/>\n\/\/\u4ece\u4ec0\u4e48\u4f4d\u7f6e\u5f00\u59cb<br \/>\nright.graphics.moveTo(this.stage.stageWidth &#8211; 40, this.stage.stageWidth \/ 2 &#8211; 10);<br \/>\n\/\/\u5230\u4ec0\u4e48\u4f4d\u7f6e\u7ed3\u675f<br \/>\nright.graphics.lineTo(this.stage.stageWidth &#8211; 40, this.stage.stageHeight &#8211; this.stage.stageWidth \/ 2 &#8211; 10);<br \/>\nright.graphics.endFill();<br \/>\nthis.addChild(right);<br \/>\nthis.right_x = this.stage.stageWidth &#8211; 40;<\/p>\n<p>let bottom: egret.Shape = new egret.Shape()<br \/>\nbottom.graphics.lineStyle(10, 0xff0304);<br \/>\nbottom.graphics.beginFill(0xff0000, 0);<br \/>\n\/\/\u7ed8\u5236 \u5706\u5fc3\u7684\u4f4d\u7f6ex,\u5706\u5fc3\u7684\u4f4d\u7f6ey,\u534a\u5f84\uff0c\u4ece\u4ec0\u4e48\u89d2\u5ea6\u5230\u4ec0\u4e48\u89d2\u5ea6\u7ed8\u5236<br \/>\nbottom.graphics.drawArc(this.stage.stageWidth \/ 2, this.stage.stageHeight &#8211; this.stage.stageWidth \/ 2 &#8211; 10, this.stage.stageWidth \/ 2 &#8211; 40, -Math.PI, 0, true)<br \/>\nbottom.graphics.endFill();<br \/>\nthis.addChild(bottom);<\/p>\n<p>this.bot_cx = this.stage.stageWidth \/ 2;<br \/>\nthis.bot_cy = this.stage.stageHeight &#8211; this.stage.stageWidth \/ 2 &#8211; 10, this.stage.stageWidth \/ 2 &#8211; 40;<br \/>\n}<br \/>\nprivate onDrive() {<br \/>\nif (!this.isMove) {<br \/>\nthis.isMove = true;<br \/>\nthis.addEventListener(egret.Event.ENTER_FRAME, this.update, this)<br \/>\n} else {<br \/>\nthis.isMove = false;<br \/>\nthis.removeEventListener(egret.Event.ENTER_FRAME, this.update, this);<br \/>\n}<br \/>\n}<br \/>\nprivate update() {<br \/>\nif (this.car.y &lt; this.top_cy) { this.topRun() } else if (this.car.y &gt; this.bot_cy) {<br \/>\nthis.bottomRun()<\/p>\n<p>} else {<br \/>\nif (this.car.x &lt; this.top_cx) {<br \/>\nthis.leftRun()<br \/>\n} else {<br \/>\nthis.rightRun()<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\nprivate leftRun() {<br \/>\nthis.cl = 0;<br \/>\nthis.car.y += this.speed;<br \/>\n}<br \/>\nprivate rightRun() {<br \/>\nthis.cl = 0;<br \/>\nthis.car.y -= this.speed;<br \/>\n}<br \/>\nprivate topRun() {<br \/>\n\/\/\u5148\u786e\u5b9a\u4f4d\u7f6e\uff0c\u7136\u540e\u6839\u636e\u4f4d\u7f6e \u8ba1\u7b97\u51fa\u89d2\u5ea6, \u5f27\u5ea6 = \u89d2\u5ea6\/\u534a\u5f84<br \/>\nthis.cl += this.speed;<br \/>\nlet angle: number = this.cl \/ this.radius;<br \/>\nif (angle &lt; Math.PI) {<br \/>\n\/\/\u6839\u636e\u5f27\u5ea6\u65cb\u8f6c\u5c0f\u8f66\u5934\u90e8<br \/>\nthis.car.rotation = 180 * -angle \/ Math.PI;<br \/>\nthis.car.x = this.top_cx + Math.cos(angle) * this.radius;<br \/>\nthis.car.y = this.top_cy &#8211; Math.sin(angle) * this.radius;<br \/>\n} else {<br \/>\nthis.car.rotation = 180;<br \/>\nthis.car.x = this.left_x;<br \/>\nthis.car.y = this.top_cy;<br \/>\n}<br \/>\n}<br \/>\nprivate bottomRun() {<br \/>\nthis.cl += this.speed;<br \/>\nlet angle: number = this.cl \/ this.radius;<br \/>\nif (angle &lt; Math.PI) { this.car.rotation = -(180 + 180 * angle \/ Math.PI); this.car.x = this.bot_cx &#8211; Math.cos(angle) * this.radius; this.car.y = this.bot_cy + Math.sin(angle) * this.radius; } else { this.car.rotation = 0; this.car.x = this.right_x; this.car.y = this.bot_cy; } } \/** * \u6839\u636ename\u5173\u952e\u5b57\u521b\u5efa\u4e00\u4e2aBitmap\u5bf9\u8c61\u3002name\u5c5e\u6027\u8bf7\u53c2\u8003resources\/resource.json\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\u3002 * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources\/resource.json. *\/ private createBitmapByName(name: string) { let result = new egret.Bitmap(); let texture: egret.Texture = RES.getRes(name); result.texture = texture; return result; } \/** * \u63cf\u8ff0\u6587\u4ef6\u52a0\u8f7d\u6210\u529f\uff0c\u5f00\u59cb\u64ad\u653e\u52a8\u753b * Description file loading is successful, start to play the animation *\/ private startAnimation(result: string[]) { let parser = new egret.HtmlTextParser(); let textflowArr = result.map(text =&gt; parser.parse(text));<br \/>\nlet textfield = this.textfield;<br \/>\nlet count = -1;<br \/>\nlet change = () =&gt; {<br \/>\ncount++;<br \/>\nif (count &gt;= textflowArr.length) {<br \/>\ncount = 0;<br \/>\n}<br \/>\nlet textFlow = textflowArr[count];<\/p>\n<p>\/\/ \u5207\u6362\u63cf\u8ff0\u5185\u5bb9<br \/>\n\/\/ Switch to described content<br \/>\ntextfield.textFlow = textFlow;<br \/>\nlet tw = egret.Tween.get(textfield);<br \/>\ntw.to({ &#8220;alpha&#8221;: 1 }, 200);<br \/>\ntw.wait(2000);<br \/>\ntw.to({ &#8220;alpha&#8221;: 0 }, 200);<br \/>\ntw.call(change, this);<br \/>\n};<\/p>\n<p>change();<br \/>\n}<br \/>\n}<\/p>\n<p>\u770b\u4e0b\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n<div style=\"width: 540px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-653-1\" width=\"540\" height=\"960\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"http:\/\/blog.xrhoujie.com\/wp-content\/uploads\/2019\/12\/car.mp4?_=1\" \/><a href=\"http:\/\/blog.xrhoujie.com\/wp-content\/uploads\/2019\/12\/car.mp4\">http:\/\/blog.xrhoujie.com\/wp-content\/uploads\/2019\/12\/car.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u521b\u5efa\u4e00\u4e2aegret\u6e38\u620f\u9879\u76ee \uff0c \u7136\u540e\u5728\u6211\u4eec\u7684main.ts\u6587\u4ef6\u5199\u4e0a\u4ee5\u4e0b\u4ee3\u7801 class Main exten [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[15],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/653"}],"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=653"}],"version-history":[{"count":1,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":655,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/655"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/143"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}