cocos Creator 动态添加图片函数

调用方法传入节点和图片地址

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">this._addSpritePic(this<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bg</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.getComponent</span>(cc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Sprite</span>), <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"pic/map/1"</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>
// 第一个必须是cc<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.Sprite</span> 类型   如果this<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.bg</span>是node节点   需要getComponent里面的Sprite</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

方法里将传入图片地址对应图片加载到对应节点(必须是cc.Sprite节点 !!!)

<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    _addSpritePic: <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(container, addres)</span>{</span>
        cc.loader.loadRes(addres, cc.SpriteFrame, <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">function</span> <span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(err, spFrame)</span> {</span>
            container.spriteFrame = spFrame           
        });
    },</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
展开阅读全文

没有更多推荐了,返回首页