{"id":508,"date":"2018-12-13T11:51:42","date_gmt":"2018-12-13T03:51:42","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=508"},"modified":"2019-01-17T17:00:48","modified_gmt":"2019-01-17T09:00:48","slug":"creator-%e6%8a%93%e5%a8%83%e5%a8%83%e6%91%87%e6%9d%86%e7%af%87","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=508","title":{"rendered":"creator \u6293\u5a03\u5a03(\u6447\u6746\u7bc7)"},"content":{"rendered":"<p>\u5e9f\u8bdd\u4e0d\u903c\u903c\u76f4\u63a5\u4e0a\u4ee3\u7801<\/p>\n<p>Main.js<br \/>\n<code><br \/>\ncc.Class({<br \/>\nextends: cc.Component,<\/code><br \/>\n<code><br \/>\nproperties: {<br \/>\nsceneScript: true,<br \/>\nrightWallLimit: {<br \/>\ndefault: 268,<br \/>\ntooltip: \"\u53f3\u4fa7\u5899\u4f53\u9650\u5236\",<br \/>\n},<br \/>\nleftWallLimit: {<br \/>\ndefault: -275,<br \/>\ntooltip: \"\u5de6\u4fa7\u5899\u4f53\u9650\u5236\",<br \/>\n},<br \/>\nposInit: {<br \/>\ndefault: 0,<br \/>\ntooltip: \"\u5750\u6807\u521d\u59cb\u5316\",<br \/>\n},<br \/>\nrocker: cc.Node,<br \/>\nclip: cc.Node,<br \/>\nrockerAudio: {<br \/>\nurl: cc.AudioClip,<br \/>\ndefault: null<br \/>\n},<br \/>\n},<br \/>\nonLoad() {<\/code><\/p>\n<p>},<br \/>\n<code>start() {<br \/>\nthis.loadStartGameBtn();<br \/>\nthis.moveRocker(this.rocker)<br \/>\n},<br \/>\nmoveRocker(n) {<br \/>\nn.on('touchstart', () =&gt; {<br \/>\n\/\/\u64ad\u653e\u6447\u6746\u97f3\u9891<br \/>\ncc.audioEngine.play(this.rockerAudio, false, 1);<br \/>\n})<br \/>\nn.on('touchmove', (ev) =&gt; {<br \/>\n\/\/\u5c06\u4e00\u4e2a\u70b9\u8f6c\u6362\u5230\u8282\u70b9 (\u5c40\u90e8) \u7a7a\u95f4\u5750\u6807\u7cfb\uff0c\u8fd9\u4e2a\u5750\u6807\u7cfb\u4ee5\u951a\u70b9\u4e3a\u539f\u70b9\u3002<br \/>\nvar a = n.parent.convertToNodeSpaceAR(ev.touch.getLocation());<br \/>\n\/\/\u89e6\u6478\u7684\u70b9\uff0c\u62d6\u62fd\u79fb\u52a8\u7684\u70b9<br \/>\nvar angle = this.getAngle(a, n) - 90<br \/>\n\/\/ console.log(angle)<br \/>\n\/\/\u533a\u57df\u5de6\u4fa7<br \/>\nif (angle &gt;= -180 &amp;&amp; angle &lt; 0) {<br \/>\n\/\/\u5de6\u534a\u5706\u4e0a<br \/>\nif (angle &gt; -45 &amp;&amp; angle &lt; 0) {<br \/>\nn.rotation = angle<br \/>\n} \/\/\u5de6\u534a\u5706\u4e0b<br \/>\nelse {<br \/>\nn.rotation = -45<br \/>\n} \/\/\u6447\u6746\u5de6\u4fa7\u5f00\u5173 \u5f00\u542f<br \/>\nthis.directionLeft = true;<br \/>\nthis.directionRight = false;<br \/>\n} else<br \/>\n{ \/\/\u53f3\u8fb9\u534a\u5706\u4e0a<br \/>\nif (angle &gt; 0 &amp;&amp; angle &lt; 45) {<br \/>\nn.rotation = angle } else { n.rotation = 45 }<br \/>\nthis.directionRight = true;<br \/>\nthis.directionLeft = false; }<br \/>\n})<br \/>\nn.on(\"touchend\", () =&gt; {<br \/>\nn.runAction(cc.rotateTo(0.3, 0))<br \/>\nthis.rockerInit();<br \/>\n})<br \/>\nn.on(\"touchcancel\", () =&gt; {<br \/>\nn.runAction(cc.rotateTo(0.3, 0))<br \/>\nthis.rockerInit();<br \/>\n})<br \/>\n},<br \/>\nrockerInit() {<br \/>\nthis.directionRight = this.posInit;<br \/>\nthis.directionLeft = this.posInit;<br \/>\n},<br \/>\nupdate: function (dt) {<br \/>\nif (this.directionLeft) {<br \/>\n\/\/\u6447\u6746\u5de6\u8fb9\u5f00\u542f<br \/>\nthis.clip.x -= 1;<br \/>\n\/\/\u9650\u5236\u722a\u5b50\u5de6\u533a\u57df<br \/>\nif (this.clip.x &lt; this.leftWallLimit)<br \/>\n{<br \/>\nthis.clip.x = this.leftWallLimit; this.rockerInit();<br \/>\n}<br \/>\n}<br \/>\n\/\/\u6447\u6746\u53f3\u8fb9\u5f00\u542f<br \/>\nif (this.directionRight) {<br \/>\nthis.clip.x += 1; \/\/\u9650\u5236\u722a\u5b50\u53f3\u533a\u57df<br \/>\nif (this.clip.x &gt; this.rightWallLimit) {<br \/>\nthis.clip.x = this.rightWallLimit;<br \/>\nthis.rockerInit();<br \/>\n}<br \/>\n}<br \/>\n},<br \/>\n});<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code><\/code><\/p>\n<p>\/\/\u8ba1\u7b97\u89d2\u5ea6<br \/>\ngetAngle(p1, p2) {<br \/>\n\/\/\u4e24\u70b9\u7684x\u3001y\u503c<br \/>\nvar x = p2.x &#8211; p1.x;<br \/>\nvar y = p2.y &#8211; p1.y;<br \/>\nvar hypotenuse = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));<br \/>\n\/\/\u659c\u8fb9\u957f\u5ea6<br \/>\nvar cos = x \/ hypotenuse;<br \/>\nvar radian = Math.acos(cos);<br \/>\n\/\/\u6c42\u51fa\u5f27\u5ea6<br \/>\nvar angle = 180 \/ (Math.PI \/ radian);<br \/>\n\/\/\u7528\u5f27\u5ea6\u7b97\u51fa\u89d2\u5ea6<br \/>\nif (y &gt; 0) {<br \/>\nangle = -angle<br \/>\n}<br \/>\nreturn a<br \/>\n<code><br \/>\n\/\/loading\u9875\u9762<br \/>\nloadStartGameBtn(func) {<br \/>\nconst splash = window.document.getElementById('splash');<br \/>\nconst startBtn = window.document.getElementById(\"start-btn\") \/\/\u5f00\u59cb\u6309\u94ae<br \/>\nstartBtn.style.display = 'inline-block';<br \/>\nstartBtn.onclick = () =&gt; {<br \/>\nsplash.style.display = 'none'<br \/>\nif (func) {<br \/>\nfunc.bind(this)()<br \/>\n}<br \/>\n}<br \/>\n}<\/code><br \/>\n<code><br \/>\n<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>demo\u5730\u5740\uff1a<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e9f\u8bdd\u4e0d\u903c\u903c\u76f4\u63a5\u4e0a\u4ee3\u7801 Main.js cc.Class({ extends: cc.Component, pr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":307,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[32],"_links":{"self":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/508"}],"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=508"}],"version-history":[{"count":6,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions"}],"predecessor-version":[{"id":523,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/508\/revisions\/523"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/media\/307"}],"wp:attachment":[{"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=508"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}