CocosCreator 动态加载资源

Cocos官方推荐用cc.loader.loadRes()方法。

三个参数:
1.资源地址
不要加上resources目录。资源的后缀格式不要写,比如png,jpg不要写入
例如:我将载入resources下的images下的man.png图片那么我应该写入的第一个参数为:images/man

2.第二个参数,用来指定你要加载的资源的类型
对于单个图片资源,资源类型为cc.SpriteFrame;音效资源为:cc.AudioClip

3.第三个参数为内置回调函数,在函数中我们可以进行自己的业务逻辑
如下代码

public SetStarTex(_sprite: cc.Sprite, _colorIndex: number) {
       let tex = this.StarTexs[_colorIndex];
       if (tex == null) {
           let texName: string = "";
           switch (_colorIndex) {
               case 0:
                   texName = "tex/star/red";
                   break;
               case 1:
                   texName = "tex/star/yellow";
                   break;
               case 2:
                   texName = "tex/star/blue";
                   break;
               case 3:
                   texName = "tex/star/green";
                   break;
               case 4:
                   texName = "tex/star/purple";
                   break;
           }
           
           cc.loader.loadRes(texName, cc.SpriteFrame, function (error, object) {
               if (error) {
                   Debug.Error("error while load " + texName);
               }
               _sprite.spriteFrame = object;
               this.StarTexs[_colorIndex] = object;
           }.bind(this));
       }
       else {
           _sprite.spriteFrame = tex;
       }

   }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iningwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值