cocos3.10 html 关于sprite 未加载的时候显示白色的问题
在做游戏的项目中,发现游戏图片未下载下来,或者正在下载中,显示为白色。
查看了引擎源码,在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、修改方案,把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; .................... } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |