cocos creator image spriteFrame dynamic loading (screen flickering)

When using pictures to load, the screen flickers.

After exclusion, it was found that when loading pictures, the pictures were loaded synchronously .

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

var texture = cc.textureCache.addImage(resUrl)

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

It is recommended to modify it to load images asynchronously

var url = 'globalUI/myHeadFrame.png';

var _this = this;

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

{  

  _this.myPlayer.spriteFrame = spriteFrame;

});

expand:

Loading pictures from the network

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;

});

Note: The dynamically loaded image resource path needs to create a resources folder and place the image in it, and the indexed file path should not include resources.

Otherwise with

spriteFrame.textureLoaded is not a function

Such a mistake.

2. Asynchronous loading of pictures is mainly cc.loader.loadRes(uurl,cc.SpriteFrame,funciton(err,spriteframe));

Among them, cc.SpriteFrame pays attention to capitalization.

Guess you like

Origin blog.csdn.net/huanghuipost/article/details/102571921