cocos3.10 html 关于sprite 未加载的时候显示白色的问题

来源:互联网 发布:淘宝视频拍摄手机软件 编辑:程序博客网 时间:2024/06/05 04:40

在做游戏的项目中,发现游戏图片未下载下来,或者正在下载中,显示为白色。


查看了引擎源码,在CCSprite.js中的setTexture函数中 分析一下

1、如果纹理不存在就在设置渲染命令中的纹理为空

2、如果能找到纹理的名字,就添加到纹理缓存里面,

3、根据纹理是否加载来处理,如果纹理竞价加载,那就直接设置纹理到渲染命令中,如果纹理未加载,就设置渲染命令中的纹理为空,就是

this._renderCmd._setTexture(null);这句,然后监听纹理加载事件,加载完毕重新设置纹理。


setTexture:function (texture) {

        if(!texture)

            returnthis._renderCmd._setTexture(null);

 

        //CCSprite.cpp 327 and 338

        var isFileName = cc.isString(texture);

 

        if(isFileName)

            texture =cc.textureCache.addImage(texture);

 

        if(texture._textureLoaded){

            this._setTexture(texture,isFileName);

            this.setColor(this._realColor);

            this._textureLoaded = true;

        }else{

            this._renderCmd._setTexture(null);

            texture.addEventListener("load",function(){

                this._setTexture(texture,isFileName);

                this.setColor(this._realColor);

                this._textureLoaded = true;

            }, this);

        }

    },



2、

因为如果把纹理设置为空,会运行,_updateBlendFunc(),其中如果纹理不存在,就会把

blendFunc.src= cc.SRC_ALPHA;并且在渲染的时候会根据这个值来渲染,就会表现为白色

 

proto._updateBlendFunc= function () {

        if (this._batchNode) {

           cc.log(cc._LogInfos.Sprite__updateBlendFunc);

            return;

        }

 

        // it's possible to have an untexturedsprite

        var node = this._node,

            blendFunc = node._blendFunc;

        if (!node._texture ||!node._texture.hasPremultipliedAlpha()) {

            if (blendFunc.src === cc.ONE&& blendFunc.dst === cc.BLEND_DST) {

                blendFunc.src = cc.SRC_ALPHA;

            }

            node.opacityModifyRGB = false;

        } else {

            if (blendFunc.src === cc.SRC_ALPHA&& blendFunc.dst === cc.BLEND_DST) {

                blendFunc.src = cc.ONE;

            }

            node.opacityModifyRGB = true;

        }

    };



3、修改方案,

在CCSprite.js 554行把this._renderCmd._setTexture(null);改为this._renderCmd._setTexture(texture);

在纹理存在且没有加载成功的时候是不会渲染的

proto.rendering= function (ctx) {

        var node = this._node, locTexture =node._texture;

        if ((locTexture&&!locTexture._textureLoaded) || this._displayedOpacity === 0)

            return;

4、再次修改

   

if (this._batchNode) {    cc.log(cc._LogInfos.Sprite__updateBlendFunc);    return;}/** * 2017-6-17修改混合算法,注释了原来的算法 */var node = this._node;if(node._texture && node._texture.hasPremultipliedAlpha()){    node._blendFunc.src = cc.BLEND_SRC;    node._blendFunc.dst = cc.BLEND_DST;    node.opacityModifyRGB = true;}else{    node._blendFunc.src = cc.SRC_ALPHA;    node._blendFunc.dst = cc.ONE_MINUS_SRC_ALPHA;    node.opacityModifyRGB = false;}

 


阅读全文
0 0