本节学习了cocos的资源加载,这次更改了CCLoaderScene里的代码(也可以在自己新建一个类定义载入界面,这里为了方便起见),将载入界面的外观进行了修改,并且在此基础上实施了纹理的添加cc.textureCache.addImage()和删除纹理cc.textureCache.dumpCachedTextureInfo()。下边是CCLoaderScene里的部分代码:
首先是init()函数:
init : function(){
var self = this;
//logo
var logoWidth = 160;
var logoHeight = 200;
// bg
var bgLayer = self._bgLayer = new cc.LayerColor(cc.color(199,21,133,255));
self.addChild(bgLayer,0);
var size = cc.winSize;
//image move to CCSceneFile.js
var fontSize = 24,lblHeight = -logoHeight / 2 + 100;
if(cc._loaderImage){//这个图片在Base64Image中保存,我们可以替换
//loading logo
cc.loader.loadImg(cc._loaderImage,{isCrossOrigin : false },function(err,img){
logoWidth = img.width;
logoHeight = img.height;
self._initStage(img,cc.visibleRect.center);
});
fontSize = 14;
lblHeight = -logoHeight / 2 - 10;
}
//loading percent加载进度
var label = self._label = new cc.LabelTTF("Loading... 0%","Arial",fontSize);
label.setPosition(cc.pAdd(cc.visibleRect.center,cc.p(0,lblHeight)));
label.setColor(cc.color(180,180,180));
bgLayer.addChild(this._label,10);
cc.textureCache.addImage("res/32636-4.png");
cc.textureCache.dumpCachedTextureInfo();
var mylogo = cc.textureCache.getTextureForKey("res/32636-4.png");
cc.log("成功加载loading界面的32636-4.png图片");
var sp = new cc.Sprite(mylogo);
sp.setPosition(size.width/2,300);
this.addChild(sp);
sp.setTag(100);
return true;
}
我们可以通过更改Base64Image里边的代码来修改载入界面的背景图。同样onEnter和onExit方法也如下:
onEnter: function () {
var self = this;
cc.Node.prototype.onEnter.call(self);
self.schedule(self._startLoading,0.3);
},
/**
* custom onExit
*/
onExit: function () {
cc.Node.prototype.onExit.call(this);
var tmpStr = "Loading... 0%";
this._label.setString(tmpStr);
cc.textureCache.removeAllTextures();
cc.textureCache.dumpCachedTextureInfo();
cc.log("加载结束,清除所有纹理");
},
在帧结束时,清理纹理。
作业传送门:
http://www.cocoscvp.com/usercode/2016_05_22/9a01ae13cb017dab984dba22839e8d1873be4fa9/