cocos creator 图片spriteFrame动态加载(屏幕闪烁)

在使用图片加载的时候,出现屏幕闪烁。

排除后发现是,在加载图片的时候,使用了同步加载图片

var resUrl = cc.url.raw('globalUI/myHeadFrame.png')

var texture = cc.textureCache.addImage(resUrl)

node.getComponent(cc.Sprite).spriteFrame.setTexture(texture)

建议修改成异步加载图片

var url = 'globalUI/myHeadFrame.png';

var _this = this;

cc.loader.loadRes(url,cc.SpriteFrame,function(err,spriteFrame)

{  

  _this.myPlayer.spriteFrame = spriteFrame;

});

拓展:

网络加载图片

var url = "http://192.168.1.120:8080/icon/HeadFrame.png";

cc.loader.load({url: url, type: 'png'}, function(err,img){    

    var myIcon  = new cc.SpriteFrame(img);      

    self.logo.spriteFrame = myIcon;

});

注意:动态加载的图片资源路径需要创建一个resources文件夹,将图片放置其中,索引的文件路径不要带resources。

否则会带有

spriteFrame.textureLoaded is not a function

这样的错误。

2、异步加载图片 主要是cc.loader.loadRes(uurl,cc.SpriteFrame,funciton(err,spriteframe));

其中的cc.SpriteFrame注意大小写。

猜你喜欢

转载自blog.csdn.net/huanghuipost/article/details/102571921
今日推荐