加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos creator动态更换纹理

发布时间:2020-12-14 17:27:33 所属栏目:百科 来源:网络整理
导读:有2种办法。 1 是预先准备好所有的纹理,然后放到编辑器中,绑定到对应的脚本。 替换的时候只需要将目标的spriteFrame=source.spriteFrame即可。 把图片拖到编辑器中 创建一个数组,创建当前的要替换纹理索引 imgIdx : 1 ,sprArray :{ default :[], type :[cc

有2种办法。
1 是预先准备好所有的纹理,然后放到编辑器中,绑定到对应的脚本。
替换的时候只需要将目标的spriteFrame=source.spriteFrame即可。

把图片拖到编辑器中

创建一个数组,创建当前的要替换纹理索引

imgIdx : 1,sprArray :{
            default:[],
            type:[cc.Sprite],
        },

把编辑器中的图片与数组绑定

在编辑器中增加2个按钮,一个减,一个加,当是减的时候向前变,加的时候向后变,把按钮的click事件绑定到脚本中。
添加如下代码

clickBtn:function(event){
        if(event.target == this.btnl.node){
            this.imgIdx =  --this.imgIdx < 1 ? 5 : this.imgIdx
        }
        else if(event.target == this.btnr.node){
            this.imgIdx = this.imgIdx++ % 5 + 1
        }
        // 方法1
        this.spr.spriteFrame = this.sprArray[this.imgIdx-1].spriteFrame
        // 方法2
        // var s = "resources/candy_0" + this.imgIdx + ".png"
        // this.spr.spriteFrame = new cc.SpriteFrame(cc.url.raw(s))
    }

第2种办法不需要预先把图片放到编辑器中,刚刚的代码里面已经贴出来了如何实现,这种办法更灵活。推荐使用。

https://www.processon.com/i/568c6ea4e4b0e51d149a085f 这个网站解决了大家开始设计阶段的问题,轻量级的各种设计模型,强烈推荐。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读