{"id":352,"date":"2017-10-27T17:45:24","date_gmt":"2017-10-27T09:45:24","guid":{"rendered":"http:\/\/blog.xrhoujie.com\/?p=352"},"modified":"2021-10-19T10:42:51","modified_gmt":"2021-10-19T02:42:51","slug":"cocos-creator%e5%9f%ba%e7%a1%80%e6%95%99%e7%a8%8b-%e4%bb%8e%e5%a4%b4%e5%bc%80%e5%a7%8b%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/blog.xrhoujie.com\/?p=352","title":{"rendered":"cocos creator\u57fa\u7840\u6559\u7a0b-\u4ece\u5934\u5f00\u59cb\uff08\u4e00\uff09"},"content":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u7684\u662fcocos creator\u65b0\u624b\u57fa\u7840\u6559\u7a0b\uff0c\u8fd9\u4e2a\u57fa\u7840\u6559\u7a0b\u63d0\u7b14\u524d\u6211\u60f3\u4e86\u5f88\u4e45\u624d\u5199\uff0c\u4e00\u662f\u8003\u8651\u6bcf\u4e2a\u4eba\u7684\u57fa\u7840\u4e0d\u540c\uff0c\u9700\u6c42\u4e5f\u4e0d\u540c\uff0c\u53ef\u80fd\u7167\u987e\u4e0d\u5230\u90e8\u5206\u4eba\u7684\u60f3\u6cd5\u4ee5\u53ca\u67d0\u4e9b\u9700\u6c42&#8230;&#8230;\u8bdd\u4e0d\u591a\u8bf4\u76f4\u5954\u4eca\u5929\u7684\u4e3b\u9898\uff0c<\/p>\n<p>\u5bf9\u4e8ecocos\u65b0\u624b\u521a\u63a5\u89e6\u4f1a\u6709\u4e9b\u62b5\u89e6\uff0c\u4f1a\u611f\u89c9\u8ff7\u832b\uff0c\u4ee5\u81f3\u4e8e\u65e0\u4ece\u4e0b\u624b\uff0c\u4e0d\u77e5\u9053\u5982\u4f55\u5f00\u59cb\uff0c\u7ed9\u5927\u5bb6\u5206\u4eablist\u5305\u62ec\uff1a<\/p>\n<p>1.\u4e00\u4e9b\u5e38\u7528\u7684\u7ec4\u4ef6\u8bbf\u95ee\u3002<\/p>\n<p>2\uff0c\u5f53\u524djs\u6587\u4ef6\u8bbf\u95ee\u5176\u4f59js\u8282\u70b9\u3002<\/p>\n<p>3\uff0c\u590d\u5236\u8282\u70b9\uff0f\u590d\u5236prefab\u3002<\/p>\n<p>4\uff0c\u9500\u6bc1\u8282\u70b9\u3002<\/p>\n<p>5\uff0c\u4e8b\u4ef6\u4fa6\u542con\u7684\u56db\u79cd\u65b9\u6cd5\u3002<\/p>\n<p>6\uff0c\u7f13\u52a8\u52a8\u753b\u5904\u7406\u4e0e(tween.js\u63d2\u4ef6\u8fd0\u7528)\u3002<\/p>\n<p>7\uff0c\u8ba1\u65f6\u5668\u7684\u64cd\u4f5c\u4e0e\u4f7f\u7528\u3002<\/p>\n<p>8\uff0c\u8d44\u6e90\u8bfb\u53d6\u4e0e\u64cd\u4f5c\u3002<\/p>\n<p>9\uff0c\u66f4\u6362\u56fe\u96c6\u64cd\u4f5c\u3002<\/p>\n<div>\n<h3><strong>\u4e00. \u83b7\u53d6\u7ec4\u4ef6\u7684\u51e0\u79cd\u5f62\u5f0f:<\/strong><\/h3>\n<div>\/\/1. \u901a\u8fc7\u5c5e\u6027\u68c0\u67e5\u5668\u88ab\u8d4b\u503c\u7684label\u7ec4\u4ef6\uff0c\u76f4\u63a5\u62ff\u5230\u5f97\u5230\u5b9e\u4f8b<\/div>\n<div>\/\/2. \u901a\u8fc7\u5c5e\u6027\u68c0\u67e5\u5668\u88ab\u8d4b\u503c\u7684label\u7ec4\u4ef6\u6240\u5728\u7684node\u8282\u70b9\uff0c\u7136\u540e\u901a\u8fc7getComponent\u83b7\u53d6<\/div>\n<div>\/\/ this.label.string = this.text;<\/div>\n<div><\/div>\n<div>\/\/3. \u83b7\u53d6\u5f53\u524dthis(node)\u8282\u70b9\u4e0a\u7684label\u7ec4\u4ef6<\/div>\n<div>\/\/ var _label = this.getComponent(cc.Label);<\/div>\n<div><\/div>\n<div>\/\/4. \u5148\u83b7\u53d6\u76ee\u6807\u7ec4\u4ef6\u6240\u5728\u7684\u8282\u70b9\uff0c\u7136\u540e\u901a\u8fc7getComponent\u83b7\u53d6\u76ee\u6807\u7ec4\u4ef6<\/div>\n<div>var_label=cc.find(&#8220;Canvas\/label&#8221;).getComponent(cc.Label);<\/div>\n<div>\u00a0\u4e4b\u540e\u8fdb\u884cconsole.log(_label);<\/div>\n<div>\u770b\u770b\u6211\u4eec\u7684\u63a7\u5236\u53f0\uff0c\u662f\u5426\u5df2\u7ecf\u8bbf\u95ee\u5230\u4e86\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u7684\u6548\u679c\u3002<\/div>\n<h3><strong>\u4e8c.\u901a\u8fc7\u5f53\u524djs\u6587\u4ef6\u6765\u8bbf\u95ee\u5176\u4ed6\u6a21\u5757\u7684\u4ee3\u7801<\/strong><\/h3>\n<div>\n<div>\n<div>\/\/&#8212;&gt;&gt;&gt;\u7528 require + \u6587\u4ef6\u540d(\u4e0d\u542b\u8def\u5f84) \u6765\u83b7\u53d6\u5230\u5176\u4ed6 \u6a21\u5757 \u7684\u5bf9\u8c61<\/div>\n<div>vartModuleData=require(&#8220;testJs&#8221;);<\/div>\n<div>tModuleData.tme_pa2=991;<\/div>\n<div>console.log(tModuleData.tme_pa2);<\/div>\n<\/div>\n<div><strong>\u4e09.\u5728\u5f53\u524d\u8282\u70b9\u4e0b\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7ec4\u4ef6<\/strong><\/div>\n<div>\n<div>varmySprite=newcc.Node().addComponent(cc.Sprite);<\/div>\n<div>mySprite.spriteFrame=this.t_sprite;<\/div>\n<div>mySprite.node.parent=this.node;<\/div>\n<div>mySprite.node.setPosition(300,200);<\/div>\n<h3><strong>\u4e09.\u590d\u5236\u8282\u70b9\/\u6216\u8005\u590d\u5236 prefab<\/strong><\/h3>\n<div>\n<div>\/\/\u590d\u5236\u8282\u70b9<\/div>\n<div>var lLabel = cc.instantiate(this.label);<\/div>\n<div>lLabel.node.parent=this.node;<\/div>\n<div>lLabel.node.setPosition(-200,0);<\/div>\n<div>\/\/\u590d\u5236prefab<\/div>\n<div>vartPrefab=cc.instantiate(this.t_prefab);<\/div>\n<div>tPrefab.parent=this.node;<\/div>\n<div>tPrefab.setPosition(-210,100);<\/div>\n<h3><strong>\u56db.\u9500\u6bc1\u8282\u70b9<\/strong><\/h3>\n<div>\n<div>\u7b2c\u4e00\u79cd\u65b9\u6cd5\uff1a\/\/&#8212;&gt;&gt;&gt; \u9500\u6bc1\u8282\u70b9(\u9500\u6bc1\u8282\u70b9\u5e76\u4e0d\u4f1a\u7acb\u523b\u53d1\u751f\uff0c\u800c\u662f\u5728\u5f53\u524d \u5e27\u903b\u8f91\u66f4\u65b0\u7ed3\u675f\u540e\uff0c\u7edf\u4e00\u6267\u884c)<\/div>\n<div>if (cc.isValid(this.label.node) ) {<\/div>\n<div>console.log(&#8220;\u6709\u6548\u5b58\u5728\uff0c\u8fdb\u884c\u6467\u6bc1&#8221;);<\/div>\n<div>this.label.destroy();<\/div>\n<div>}else{<\/div>\n<div>console.log(&#8220;\u5df2\u6467\u6bc1&#8221;);<\/div>\n<div>}<\/div>\n<div>\u7b2c\u4e8c\u79cd\uff1aremoveFromParent();\u79fb\u9664\u540e\u4fdd\u7559\u5728\u5185\u5b58\u4e2d<\/div>\n<div>\u7b2c\u4e09\u79cd: destroy(); \u5f7b\u5e95\u4ece\u5185\u5b58\u5220\u9664\u6389<\/div>\n<div>\n<div>\n<h3><strong>\u4e94.\u4e8b\u4ef6\u76d1\u542c on 4\u79cd\u5f62\u5f0f<\/strong><\/h3>\n<\/div>\n<div>\n<div>vartFun=function (event){<\/div>\n<div>console.log(&#8220;touchend event&#8221;);<\/div>\n<div>};<\/div>\n<div>this.node.on(&#8220;touchend&#8221;,tFun);<\/div>\n<div><\/div>\n<div>this.node.on(&#8220;touchend&#8221;,function (event){<\/div>\n<div>console.log(&#8220;touchend event&#8221;);<\/div>\n<div>});<\/div>\n<div><\/div>\n<div>this.node.on(&#8220;touchend&#8221;,function (event){<\/div>\n<div>console.log(&#8220;touchend event&#8221;);<\/div>\n<div>},this);<\/div>\n<div><\/div>\n<div>this.node.on(&#8220;touchend&#8221;,function (event){<\/div>\n<div>console.log(&#8220;touchend event&#8221;);<\/div>\n<div>}.bind(this));<\/div>\n<div><\/div>\n<div>\/\/ &#8212;&gt;&gt;&gt; \u4e00\u6b21\u6027\u7684\u4e8b\u4ef6\u76d1\u542c once<\/div>\n<div>this.node.once(&#8220;touchend&#8221;,function (event){<\/div>\n<div>console.log(&#8220;touchend event&#8221;);<\/div>\n<div>});<\/div>\n<h3><strong>\u516d.\u7f13\u52a8\u52a8\u753b\u5904\u7406( tween.js\u63d2\u4ef6)<\/strong><\/h3>\n<div>\n<div>\u4e00\u6b21\u89e6\u53d1<\/div>\n<div>letcapModule=cc.moveTo(1,cc.p(-312,-84));\/\/\u7269\u54c12\u4f4d\u79fb\u5230\u6307\u5b9a\u4f4d\u7f6e<\/div>\n<div>cap_2.runAction(capModule);<\/div>\n<div>\u4e0d\u65ad\u91cd\u590d\u591a\u6b21\u89e6\u53d1<\/div>\n<div>\n<div>this.setHand.runAction(<\/div>\n<div>cc.sequence(cc.scaleTo(0.8, 0.8),<\/div>\n<div>cc.scaleTo(1, 1))).repeatForever();<\/div>\n<h3><strong>\u4e03.\u8ba1\u65f6\u5668\u7684\u4f7f\u7528<\/strong><\/h3>\n<div>\n<div>\u591a\u6b21\u8ba1\u65f6\u5668<\/div>\n<div>ar interval = 1,repeat = 0, delay = 3;\/\/ 1s ,\u91cd\u590d\u6b21\u65701\u00a0 ,3s\u5f00\u59cb\u5ef6\u65f6<\/div>\n<div>this.schedule(function() {<\/div>\n<div>console.log(123);<\/div>\n<div>})<\/div>\n<div>\n<div>\/\/\u4e00\u6b21\u6027\u7684\u8ba1\u65f6\u5668<\/div>\n<div>varmySch=function(){ console.log(&#8220;schedule Once log&#8230;&#8221;); }<\/div>\n<div>this.scheduleOnce(mySch);<\/div>\n<div>\n<div>\/\/\u53d6\u6d88\u5b9a\u65f6\u5668<\/div>\n<div>this.unschedule(mySch);<\/div>\n<h3><strong>\u516b.\u8d44\u6e90\u8bfb\u53d6<\/strong><\/h3>\n<div>\n<div>\/\/&#8212;&gt;&gt;&gt; url raw\u8d44\u6e90\u83b7\u53d6<\/div>\n<div>varmSf=newcc.Node().addComponent(cc.Sprite);<\/div>\n<div>mSf.spriteFrame=this.t_sprite;<\/div>\n<div>mSf.spriteFrame.setTexture(this.t_url);<\/div>\n<div>mSf.node.setPosition(400,0);<\/div>\n<div>mSf.node.parent=this.node;<\/div>\n<div>mSf.node.setScale(0.5);<\/div>\n<div><\/div>\n<div>\/\/\u83b7\u5f97 Raw Asset \u7684 url<\/div>\n<div>varmUrl=cc.textureCache.addImage(cc.url.raw(&#8220;himi.png&#8221;));<\/div>\n<div>console.log(&#8220;raw asset url:&#8221;+mUrl);<\/div>\n<h3><strong>\u4e5d.\u80cc\u666f\u56fe\u7247\u83b7\u53d6\u4e0e\u66f4\u66ff<\/strong><\/h3>\n<div>\n<div>\n<div>changeBg:function(){ \/\/\u66f4\u6362\u80cc\u666f<\/div>\n<div>varsprite_pic=cc.find(&#8220;Canvas\/background&#8221;);<\/div>\n<div>varchangePic=cc.url.raw(&#8216;resources\/bg_s.png&#8217;);<\/div>\n<div>vartexture=cc.textureCache.addImage(changePic);<\/div>\n<div>sprite_pic.getComponent(cc.Sprite).spriteFrame.setTexture(texture);<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<div>\u8fd9\u7bc7\u6587\u7ae0\u5199\u51fa\u6765\u8bb2\u771f\u90fd\u662f\u6211\u4e00\u6b65\u4e00\u6b65\u8d70\u51fa\u6765\u7684\uff0c\u6216\u8bb8\u4f60\u6b63\u5728\u8ff7\u832b\uff0c\u6b63\u5728\u4e0d\u77e5\u4ece\u4f55\u4e0b\u624b\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u5230\u4f60\uff0c\u5f00\u6e90\u8ba9\u4e16\u754c\u66f4\u52a0\u7f8e\u597d\uff0c\u8f6c\u8f7d\u8bf7\u547d\u540d\u51fa\u5904\uff0c\u8c22\u8c22\u3002<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u7ed9\u5927\u5bb6\u5206\u4eab\u7684\u662fcocos creator\u65b0\u624b\u57fa\u7840\u6559\u7a0b\uff0c\u8fd9\u4e2a\u57fa\u7840\u6559\u7a0b\u63d0\u7b14\u524d\u6211\u60f3\u4e86\u5f88\u4e45\u624d\u5199\uff0c\u4e00\u662f\u8003\u8651\u6bcf [&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\/352"}],"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=352"}],"version-history":[{"count":2,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/352\/revisions"}],"predecessor-version":[{"id":825,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=\/wp\/v2\/posts\/352\/revisions\/825"}],"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=352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=352"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xrhoujie.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}