Skip to content

Commit f2e6521

Browse files
Merge branch 'develop' of https://github.com/cocos2d/cocos2d-html5 into issue2261_LoaderBug
Conflicts: cocos2d/core/CCLoader.js
1 parent 70475ed commit f2e6521

File tree

5 files changed

+53
-89
lines changed

5 files changed

+53
-89
lines changed

cocos2d/audio/SimpleAudioEngine.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,21 @@ cc.AudioEngine = cc.Class.extend(/** @lends cc.AudioEngine# */{
123123
/**
124124
* Preload music resource.
125125
* @param {String} path
126+
* @param {Function} selector
127+
* @param {Object} target
126128
*/
127-
preloadMusic:function(path){
128-
this.preloadSound(path);
129+
preloadMusic:function(path, selector, target){
130+
this.preloadSound(path, selector, target);
129131
},
130132

131133
/**
132134
* Preload effect resource.
133135
* @param {String} path
136+
* @param {Function} selector
137+
* @param {Object} target
134138
*/
135-
preloadEffect:function(path){
136-
this.preloadSound(path);
139+
preloadEffect:function(path, selector, target){
140+
this.preloadSound(path, selector, target);
137141
},
138142

139143
/**
@@ -221,8 +225,10 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
221225
* Preload music resource.<br />
222226
* This method is called when cc.Loader preload resources.
223227
* @param {String} path The path of the music file with filename extension.
228+
* @param {Function} selector
229+
* @param {Object} target
224230
*/
225-
preloadSound:function (path) {
231+
preloadSound:function (path, selector, target) {
226232
if (this._soundSupported) {
227233
var realPath = this._resPath + path;
228234
var extName = this._getExtFromFullPath(path);
@@ -234,12 +240,12 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
234240
sfxCache.audio = new Audio(realPath);
235241
sfxCache.audio.preload = 'auto';
236242
var soundPreloadCanplayHandler = function () {
237-
cc.Loader.getInstance().onResLoaded();
243+
cc.doCallback(selector, target);
238244
this.removeEventListener('canplaythrough', soundPreloadCanplayHandler, false);
239245
this.removeEventListener('error', soundPreloadErrorHandler, false);
240246
};
241247
var soundPreloadErrorHandler = function (e) {
242-
cc.Loader.getInstance().onResLoadingErr(e.srcElement.src);
248+
cc.doCallback(selector, target,e.srcElement.src);
243249
this.removeEventListener('canplaythrough', soundPreloadCanplayHandler, false);
244250
this.removeEventListener('error', soundPreloadErrorHandler, false);
245251
};
@@ -252,7 +258,7 @@ cc.SimpleAudioEngine = cc.AudioEngine.extend(/** @lends cc.SimpleAudioEngine# */
252258
}
253259
}
254260
}
255-
cc.Loader.getInstance().onResLoaded();
261+
cc.doCallback(selector, target);
256262
},
257263

258264
/**
@@ -1309,8 +1315,10 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
13091315
* Preload music resource.<br />
13101316
* This method is called when cc.Loader preload resources.
13111317
* @param {String} path The path of the music file with filename extension.
1318+
* @param {Function} selector
1319+
* @param {Object} target
13121320
*/
1313-
preloadSound: function(path) {
1321+
preloadSound: function(path, selector, target) {
13141322
if (!this._soundSupported)
13151323
return;
13161324

@@ -1319,7 +1327,7 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
13191327

13201328
// not supported, already loaded, already loading
13211329
if (this._audioData[keyName] || this._audiosLoading[keyName] || !this.isFormatSupported(extName)) {
1322-
cc.Loader.getInstance().onResLoaded();
1330+
cc.doCallback(selector, target);
13231331
return;
13241332
}
13251333

@@ -1329,11 +1337,11 @@ cc.WebAudioEngine = cc.AudioEngine.extend(/** @lends cc.WebAudioEngine# */{
13291337
// resource fetched, in @param buffer
13301338
engine._audioData[keyName] = buffer;
13311339
delete engine._audiosLoading[keyName];
1332-
cc.Loader.getInstance().onResLoaded();
1340+
cc.doCallback(selector, target);
13331341
}, function() {
13341342
// resource fetching failed
13351343
delete engine._audiosLoading[keyName];
1336-
cc.Loader.getInstance().onResLoadingErr(path);
1344+
cc.doCallback(selector, target, path);
13371345
});
13381346
},
13391347

cocos2d/core/CCLoader.js

+17-55
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
8888
return;
8989
}
9090
var res = resources.concat([]);
91-
this._resQueue.push(new cc.ResData(res, selector, target));
91+
var data = new cc.ResData(res, selector, target);
92+
this._resQueue.push(data);
9293

9394
if (!this._running) {
9495
this._running = true;
@@ -101,39 +102,14 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
101102
this._isAsync = isAsync;
102103
},
103104

104-
registerWithType:function(arrType, loader){
105-
if(arrType instanceof Array){
106-
for (var i = 0; i < arrType.length; i++) {
107-
var type = arrType[i];
108-
this._regisiterLoader[type] = loader;
109-
}
110-
}
111-
else if(typeof arrType == 'string'){
112-
this._regisiterLoader[arrType] = loader;
113-
}
114-
else {
115-
cc.log("cocos2d:unkown loader type:" + arrType);
116-
}
117-
},
118-
119-
/**
120-
* Callback when a resource file load failed.
121-
* @example
122-
* //example
123-
* cc.Loader.getInstance().onResLoaded();
124-
*/
125-
onResLoadingErr: function (name) {
126-
this._curData.loadedNumber++;
127-
cc.log("cocos2d:Failed loading resource: " + name);
128-
},
129-
130105
/**
131106
* Callback when a resource file loaded.
132-
* @example
133-
* //example
134-
* cc.Loader.getInstance().onResLoaded();
135107
*/
136-
onResLoaded: function () {
108+
onResLoaded: function (err) {
109+
if(err != null){
110+
cc.log("cocos2d:Failed loading resource: " + err);
111+
}
112+
137113
this._curData.loadedNumber++;
138114
},
139115

@@ -216,9 +192,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
216192
}
217193

218194
var percent = this.getPercentage();
219-
console.log(percent)
220195
if(percent >= 100){
221-
console.log("complete")
222196
this._complete();
223197
if (this._resQueue.length > 0) {
224198
this._running = true;
@@ -239,28 +213,27 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
239213

240214
var resInfo = this._curData.resList.shift(),
241215
path = this._getResPath(resInfo),
242-
type = this._getResType(resInfo, path),
243-
cb = this.onResLoaded.bind(this);
216+
type = this._getResType(resInfo, path);
244217

245218
switch (type) {
246219
case "IMAGE":
247-
sharedTextureCache.addImageAsync(path, cb);
220+
sharedTextureCache.addImageAsync(path, this.onResLoaded, this);
248221
break;
249222
case "SOUND":
250223
if (!sharedEngine) throw "Can not find AudioEngine! Install it, please.";
251-
sharedEngine.preloadSound(path, cb);
224+
sharedEngine.preloadSound(path, this.onResLoaded, this);
252225
break;
253226
case "XML":
254-
sharedParser.preloadPlist(path, cb);
227+
sharedParser.preloadPlist(path, this.onResLoaded, this);
255228
break;
256229
case "BINARY":
257-
sharedFileUtils.preloadBinaryFileData(path, cb);
230+
sharedFileUtils.preloadBinaryFileData(path, this.onResLoaded, this);
258231
break;
259232
case "TEXT" :
260-
sharedFileUtils.preloadTextFileData(path, cb);
233+
sharedFileUtils.preloadTextFileData(path, this.onResLoaded, this);
261234
break;
262235
case "FONT":
263-
this._registerFaceFont(resInfo, cb);
236+
this._registerFaceFont(resInfo, this.onResLoaded, this);
264237
break;
265238
default:
266239
throw "cocos2d:unknown filename extension: " + type;
@@ -293,15 +266,10 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
293266
},
294267

295268
_complete: function () {
296-
cc.doCallback(this._selector, this._target);
297-
298-
this._curData = null;
299-
if (this._resQueue.length > 0) {
300-
this._schedulePreload();
301-
}
269+
cc.doCallback(this._curData.selector,this._curData.target);
302270
},
303271

304-
_registerFaceFont: function (fontRes) {
272+
_registerFaceFont: function (fontRes,seletor,target) {
305273
var srcArr = fontRes.src;
306274
var fileUtils = cc.FileUtils.getInstance();
307275
if (srcArr && srcArr.length > 0) {
@@ -326,7 +294,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
326294
preloadDiv.style.top = "-100px";
327295
document.body.appendChild(preloadDiv);
328296
}
329-
cc.Loader.getInstance().onResLoaded();
297+
cc.doCallback(seletor,target);
330298
},
331299

332300
_unregisterFaceFont: function (fontRes) {
@@ -398,12 +366,6 @@ cc.Loader.purgeCachedData = function (resources) {
398366
}
399367
};
400368

401-
cc.Loader.register = function(typeArr, loader){
402-
if (this._instance) {
403-
this._instance.registerWithType(typeArr, loader);
404-
}
405-
};
406-
407369
/**
408370
* Returns a shared instance of the loader
409371
* @function

cocos2d/core/platform/CCFileUtils.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ cc.FileUtils = cc.Class.extend({
177177
delete this._fileDataCache[fileUrl];
178178
},
179179

180-
preloadBinaryFileData:function (fileUrl) {
180+
preloadBinaryFileData:function (fileUrl, selector, target) {
181181
fileUrl = this.fullPathForFilename(fileUrl);
182182
var selfPointer = this;
183183

@@ -193,9 +193,9 @@ cc.FileUtils = cc.Class.extend({
193193
if (fileContents)
194194
selfPointer._fileDataCache[fileUrl] = selfPointer._stringConvertToArray(fileContents);
195195
} else {
196-
cc.Loader.getInstance().onResLoadingErr(fileUrl);
196+
cc.doCallback(selector, target, fileUrl);
197197
}
198-
cc.Loader.getInstance().onResLoaded();
198+
cc.doCallback(selector, target);
199199
}
200200
};
201201
} else {
@@ -207,9 +207,9 @@ cc.FileUtils = cc.Class.extend({
207207
if (fileContents) {
208208
selfPointer._fileDataCache[fileUrl] = selfPointer._stringConvertToArray(fileContents);
209209
} else {
210-
cc.Loader.getInstance().onResLoadingErr(fileUrl);
210+
cc.doCallback(selector, target, fileUrl);
211211
}
212-
cc.Loader.getInstance().onResLoaded();
212+
cc.doCallback(selector, target);
213213
};
214214
}
215215
xhr.send(null);
@@ -264,7 +264,7 @@ cc.FileUtils = cc.Class.extend({
264264
delete this._textFileCache[fileUrl];
265265
},
266266

267-
preloadTextFileData:function (fileUrl) {
267+
preloadTextFileData:function (fileUrl, selector, target) {
268268
fileUrl = this.fullPathForFilename(fileUrl);
269269
var selfPointer = this;
270270

@@ -280,9 +280,9 @@ cc.FileUtils = cc.Class.extend({
280280
if (fileContents)
281281
selfPointer._textFileCache[fileUrl] = fileContents;
282282
} else {
283-
cc.Loader.getInstance().onResLoadingErr(fileUrl);
283+
cc.doCallback(selector, target,fileUrl);
284284
}
285-
cc.Loader.getInstance().onResLoaded();
285+
cc.doCallback(selector, target);
286286
}
287287
};
288288
} else {
@@ -292,9 +292,9 @@ cc.FileUtils = cc.Class.extend({
292292
if (xhr.responseText) {
293293
selfPointer._textFileCache[fileUrl] = xhr.responseText;
294294
} else {
295-
cc.Loader.getInstance().onResLoadingErr(fileUrl);
295+
cc.doCallback(selector, target,fileUrl);
296296
}
297-
cc.Loader.getInstance().onResLoaded();
297+
cc.doCallback(selector, target);
298298
};
299299
}
300300
xhr.send(null);

cocos2d/core/platform/CCSAXParser.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ cc.SAXParser = cc.Class.extend(/** @lends cc.SAXParser# */{
172172
* Preload plist file
173173
* @param {String} filePath
174174
*/
175-
preloadPlist: function (filePath) {
175+
preloadPlist: function (filePath, selector, target) {
176176
filePath = cc.FileUtils.getInstance().fullPathForFilename(filePath);
177177

178178
if (window.XMLHttpRequest) {
@@ -186,12 +186,11 @@ cc.SAXParser = cc.Class.extend(/** @lends cc.SAXParser# */{
186186
xmlhttp.onreadystatechange = function () {
187187
if (xmlhttp.readyState == 4) {
188188
if (xmlhttp.responseText) {
189-
cc.Loader.getInstance().onResLoaded();
189+
cc.doCallback(selector, target);
190190
that._xmlDict[filePath] = xmlhttp.responseText;
191191
xmlhttp = null;
192192
} else {
193-
cc.Loader.getInstance().onResLoaded();
194-
cc.log("cocos2d:There was a problem retrieving the xml data:" + xmlhttp.statusText);
193+
cc.doCallback(selector, target, filePath);
195194
}
196195
}
197196
};

cocos2d/core/textures/CCTextureCache.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,6 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
295295

296296
// Use same function for all load image error event callback
297297
_loadErrorHandler: function(path, textureCache, removeFrom) {
298-
cc.Loader.getInstance().onResLoadingErr(path);
299298
//remove from cache
300299
if (removeFrom[path])
301300
delete removeFrom[path];
@@ -318,12 +317,10 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
318317
texture.handleLoadedTexture();
319318
else if(textureCache._textures[texture])
320319
textureCache._textures[texture].handleLoadedTexture();
321-
cc.Loader.getInstance().onResLoaded();
322320
this.removeEventListener('load', textureCache._addAsyncLoadHandler, false);
323321
},
324322

325323
_beforeRendererLoadHandler: function (path, textureCache) {
326-
cc.Loader.getInstance().onResLoaded();
327324
var loading = textureCache._loadingTexturesBefore;
328325
if(loading[path]) {
329326
textureCache._loadedTexturesBefore[path] = loading[path];
@@ -335,14 +332,14 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
335332
/**
336333
* Loading the images asynchronously
337334
* @param {String} path
338-
* @param {cc.Node} target
339335
* @param {Function} selector
336+
* @param {Object} target
340337
* @return {cc.Texture2D}
341338
* @example
342339
* //example
343340
* cc.TextureCache.getInstance().addImageAsync("hello.png", this, this.loadingCallBack);
344341
*/
345-
addImageAsync:function (path, target, selector) {
342+
addImageAsync:function (path, selector, target) {
346343
if(!path)
347344
throw "cc.TextureCache.addImageAsync(): path should be non-null";
348345
path = cc.FileUtils.getInstance().fullPathForFilename(path);
@@ -404,9 +401,7 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
404401
var texture = this._textures[path];
405402
var image;
406403
if (texture) {
407-
if (texture.isLoaded()) {
408-
cc.Loader.getInstance().onResLoaded();
409-
} else {
404+
if (!texture.isLoaded()) {
410405
image = texture.getHtmlElementObj();
411406
image.addEventListener("load", this._preloadHandler.bind(image, texture, this));
412407
}

0 commit comments

Comments
 (0)