1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { this._super(); cc.textureCache.addImage("res/32636-2.png"); var texture1=cc.textureCache.getTextureForKey("res/32636-2.png"); cc.textureCache.dumpCachedTextureInfo(); var sp=new cc.Sprite(texture1); this.addChild(sp); sp.setPosition(cc.winSize.width/2,cc.winSize.height/2); return true; } });
var HelloWorldScene = cc.Scene.extend({ onEnter:function () { this._super(); var layer = new HelloWorldLayer(); this.addChild(layer); } });
var MyLoadScene = cc.Scene.extend({ _interval : null, _label : null, _npc:null, _className:"LoaderScene", cb: null, target: null, npc:null,
init : function(){ var self = this;
var logoWidth = 160; var logoHeight = 200;
var bgLayer = self._bgLayer = new cc.LayerColor(cc.color(32, 255, 32, 255)); self.addChild(bgLayer, 0);
var fontSize = 24, lblHeight = -logoHeight / 2 + 100; if(cc._loaderImage){ 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; } 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-2.png"); cc.textureCache.dumpCachedTextureInfo(); cc.log("loading界面添加纹理32636-2"); var npcTexture = cc.textureCache.getTextureForKey("res/32636-2.png"); var npc = self._npc = new cc.Sprite(npcTexture); npc.setPosition(npc.width/2,150); npc.setScale(0.5); this._bgLayer.addChild(this._npc,10); return true; }, _initStage: function (img, centerPos) { var self = this; var texture2d = self._texture2d = new cc.Texture2D(); texture2d.initWithElement(img); texture2d.handleLoadedTexture(); var logo = self._logo = new cc.Sprite(texture2d); logo.setScale(cc.contentScaleFactor()); logo.x = centerPos.x; logo.y = centerPos.y; self._bgLayer.addChild(logo, 10); },
onEnter: function () { var self = this; cc.Node.prototype.onEnter.call(self); self.schedule(self._startLoading, 0.3); },
onExit: function () { cc.Node.prototype.onExit.call(this); var tmpStr = "Loading... 100%"; this._label.setString(tmpStr); cc.textureCache.removeAllTextures(); cc.textureCache.dumpCachedTextureInfo(); cc.log("loading界面结束,清理纹理缓存"); },
initWithResources: function (resources, cb, target) { if(cc.isString(resources)) resources = [resources]; this.resources = resources || []; this.cb = cb; this.target = target; },
_startLoading: function () { var self = this; self.unschedule(self._startLoading); var res = self.resources; cc.loader.load(res, function (result, count, loadedCount) { var percent = (loadedCount / count * 100) | 0; percent = Math.min(percent, 100); self._label.setString("Loading... " + percent + "%"); self._npc.setPositionX(self._npc.width/2+cc.winSize.width*(percent/100)); cc.log("loading界面npc走到了"+percent+",横坐标"+(self._npc.width/2+cc.winSize.width*(percent/100))+""); }, function () { if (self.cb) self.cb.call(self.target); }); },
_updateTransform: function(){ this._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty); this._bgLayer._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty); this._label._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty); this._logo._renderCmd.setDirtyFlag(cc.Node._dirtyFlags.transformDirty); } });
MyLoadScene.preload = function(resources, cb, target){ var _cc = cc; if(!_cc.loaderScene) { _cc.loaderScene = new MyLoadScene(); _cc.loaderScene.init(); cc.eventManager.addCustomListener(cc.Director.EVENT_PROJECTION_CHANGED, function(){ _cc.loaderScene._updateTransform(); }); } _cc.loaderScene.initWithResources(resources, cb, target); cc.director.runScene(_cc.loaderScene); return _cc.loaderScene; };
|