Skip to content

Commit 70475ed

Browse files
Merge branch 'develop' of https://github.com/cocos2d/cocos2d-html5 into issue2261_LoaderBug
Conflicts: cocos2d/core/CCLoader.js
2 parents ac388bf + fef9db5 commit 70475ed

File tree

13 files changed

+87
-87
lines changed

13 files changed

+87
-87
lines changed

cocos2d/CCNotificationCenter.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,7 @@ cc.NotificationObserver = cc.Class.extend({
143143
* @param {cc.Class} obj
144144
*/
145145
performSelector:function (obj) {
146-
if (this._target && (typeof(this._selector) == "string")) {
147-
this._target[this._selector](obj);
148-
} else if (this._target && (typeof(this._selector) == "function")) {
149-
this._selector.call(this._target, obj);
150-
} else {
151-
this._selector(obj);
152-
}
146+
cc.doCallback(this._selector, this._target,obj);
153147
},
154148

155149
_target:null,

cocos2d/actions/CCActionInstant.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,7 @@ cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{
399399
* execute the function.
400400
*/
401401
execute:function () {
402-
if (this._callFunc != null) //CallFunc, N, ND
403-
this._callFunc.call(this._selectorTarget, this._target, this._data);
404-
else if(this._function)
405-
this._function.call(null, this._target);
402+
cc.doCallback(this._callFunc, this._selectorTarget,this._data);
406403
},
407404

408405
/**

cocos2d/core/CCLoader.js

+57-51
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ cc.ResData = function (resList, selector, target) {
6363
cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
6464
_curData: null,
6565
_resQueue: null,
66-
_animationInterval: 1 / 60,
67-
_interval: null,
6866
_isAsync: false,
6967
_scheduler: null,
7068
_running: false,
69+
_regisiterLoader: false,
7170

7271
/**
7372
* Constructor
@@ -88,15 +87,35 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
8887
cc.log("cocos2d:resources should not null");
8988
return;
9089
}
90+
var res = resources.concat([]);
91+
this._resQueue.push(new cc.ResData(res, selector, target));
9192

92-
this._resQueue.push(new cc.ResData(resources, selector, target));
93-
this._schedulePreload();
93+
if (!this._running) {
94+
this._running = true;
95+
this._curData = this._resQueue.shift();
96+
this._scheduler.scheduleUpdateForTarget(this);
97+
}
9498
},
9599

96100
setAsync: function (isAsync) {
97101
this._isAsync = isAsync;
98102
},
99103

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+
100119
/**
101120
* Callback when a resource file load failed.
102121
* @example
@@ -127,12 +146,10 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
127146
*/
128147
getPercentage: function () {
129148
var percent = 0, curData = this._curData;
130-
if (!curData) {
131-
percent = 0;
132-
}
133-
else if (curData.totalNumber == 0) {
149+
if (curData.totalNumber == 0) {
134150
percent = 100;
135-
} else {
151+
}
152+
else {
136153
percent = (0 | (curData.loadedNumber / curData.totalNumber * 100));
137154
}
138155
return percent;
@@ -183,8 +200,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
183200
}
184201
},
185202

186-
_preload: function () {
187-
this._updatePercent();
203+
update: function () {
188204
if (this._isAsync) {
189205
var frameRate = cc.Director.getInstance()._frameRate;
190206
if (frameRate != null && frameRate < 20) {
@@ -194,14 +210,25 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
194210
}
195211

196212
var curData = this._curData;
197-
if (!curData && this._resQueue.length > 0) {
198-
this._curData = this._resQueue.shift();
199-
}
200-
201213
if (curData && curData.curNumber < curData.totalNumber) {
202214
this._loadRes();
203215
curData.curNumber++;
204216
}
217+
218+
var percent = this.getPercentage();
219+
console.log(percent)
220+
if(percent >= 100){
221+
console.log("complete")
222+
this._complete();
223+
if (this._resQueue.length > 0) {
224+
this._running = true;
225+
this._curData = this._resQueue.shift();
226+
}
227+
else{
228+
this._running = false;
229+
this._scheduler.unscheduleUpdateForTarget(this);
230+
}
231+
}
205232
},
206233

207234
_loadRes: function () {
@@ -212,46 +239,35 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
212239

213240
var resInfo = this._curData.resList.shift(),
214241
path = this._getResPath(resInfo),
215-
type = this._getResType(resInfo, path);
242+
type = this._getResType(resInfo, path),
243+
cb = this.onResLoaded.bind(this);
216244

217245
switch (type) {
218246
case "IMAGE":
219-
sharedTextureCache.addImage(path);
247+
sharedTextureCache.addImageAsync(path, cb);
220248
break;
221249
case "SOUND":
222250
if (!sharedEngine) throw "Can not find AudioEngine! Install it, please.";
223-
sharedEngine.preloadSound(path);
251+
sharedEngine.preloadSound(path, cb);
224252
break;
225253
case "XML":
226-
sharedParser.preloadPlist(path);
254+
sharedParser.preloadPlist(path, cb);
227255
break;
228256
case "BINARY":
229-
sharedFileUtils.preloadBinaryFileData(path);
257+
sharedFileUtils.preloadBinaryFileData(path, cb);
230258
break;
231259
case "TEXT" :
232-
sharedFileUtils.preloadTextFileData(path);
260+
sharedFileUtils.preloadTextFileData(path, cb);
233261
break;
234262
case "FONT":
235-
this._registerFaceFont(resInfo);
263+
this._registerFaceFont(resInfo, cb);
236264
break;
237265
default:
238266
throw "cocos2d:unknown filename extension: " + type;
239267
break;
240268
}
241269
},
242270

243-
_schedulePreload: function () {
244-
if (!this._running) {
245-
this._running = true;
246-
this._scheduler.scheduleCallbackForTarget(this, this._preload);
247-
}
248-
},
249-
250-
_unschedulePreload: function () {
251-
this._running = false;
252-
this._scheduler.unscheduleCallbackForTarget(this, this._preload);
253-
},
254-
255271
_getResPath: function (resInfo) {
256272
return typeof resInfo == "string" ? resInfo : resInfo.src;
257273
},
@@ -276,24 +292,8 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
276292
}
277293
},
278294

279-
_updatePercent: function () {
280-
var percent = this.getPercentage();
281-
282-
if (percent >= 100) {
283-
this._unschedulePreload();
284-
this._complete();
285-
}
286-
},
287-
288295
_complete: function () {
289-
var _target = this._curData.target, _selector = this._curData.selector;
290-
if (_target && (typeof(_selector) == "string")) {
291-
_target[_selector](this);
292-
} else if (_target && (typeof(_selector) == "function")) {
293-
_selector.call(_target, this);
294-
} else {
295-
_selector(this);
296-
}
296+
cc.doCallback(this._selector, this._target);
297297

298298
this._curData = null;
299299
if (this._resQueue.length > 0) {
@@ -398,6 +398,12 @@ cc.Loader.purgeCachedData = function (resources) {
398398
}
399399
};
400400

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

cocos2d/core/CCScheduler.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,7 @@ cc.Timer = cc.Class.extend(/** @lends cc.Timer# */{
316316
},
317317

318318
_callSelector:function(){
319-
if (typeof(this._selector) == "string")
320-
this._target[this._selector](this._elapsed);
321-
else // if (typeof(this._selector) == "function") {
322-
this._selector.call(this._target, this._elapsed);
319+
cc.doCallback(this._selector, this._target,this._elapsed);
323320
},
324321

325322
/**

cocos2d/core/platform/CCCommon.js

+18
Original file line numberDiff line numberDiff line change
@@ -554,4 +554,22 @@ cc.SizeFromString = function (content) {
554554
var result = CCNS_REG1.exec(content);
555555
if(!result) return cc.SizeZero();
556556
return cc.size(parseFloat(result[1]), parseFloat(result[2]));
557+
};
558+
559+
/**
560+
*
561+
* @param {Function} selector
562+
* @param {cc.Node} target
563+
* @param {Object|Number|String} data
564+
*/
565+
cc.doCallback = function (selector, target, data) {
566+
if(!selector)
567+
return ;
568+
if (target && (typeof(selector) == "string")) {
569+
target[selector](data);
570+
} else if (target && (typeof(selector) == "function")) {
571+
selector.call(target, data);
572+
} else {
573+
selector(data);
574+
}
557575
};

cocos2d/core/sprite_nodes/CCSprite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ cc.Sprite = cc.NodeRGBA.extend(/** @lends cc.Sprite# */{
350350
var locListeners = this._loadedEventListeners;
351351
for(var i = 0, len = locListeners.length; i < len; i++){
352352
var selCallback = locListeners[i];
353-
selCallback.eventCallback.call(selCallback.eventTarget, this);
353+
cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
354354
}
355355
locListeners.length = 0;
356356
},

cocos2d/core/sprite_nodes/CCSpriteFrame.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{
8080
if (!locListeners) return;
8181
for(var i = 0, len = locListeners.length; i < len; i++){
8282
var selCallback = locListeners[i];
83-
selCallback.eventCallback.call(selCallback.eventTarget, this);
83+
cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
8484
}
8585
locListeners.length = 0;
8686
},

cocos2d/core/textures/CCTexture2D.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ cc.Texture2DWebGL = cc.Class.extend(/** @lends cc.Texture2D# */{
932932
var locListeners = this._loadedEventListeners;
933933
for (var i = 0, len = locListeners.length; i < len; i++) {
934934
var selCallback = locListeners[i];
935-
selCallback.eventCallback.call(selCallback.eventTarget, this);
935+
cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
936936
}
937937
locListeners.length = 0;
938938
}
@@ -1284,7 +1284,7 @@ cc.Texture2DCanvas = cc.Class.extend(/** @lends cc.Texture2D# */{
12841284
var locListeners = this._loadedEventListeners;
12851285
for(var i = 0, len = locListeners.length; i < len; i++){
12861286
var selCallback = locListeners[i];
1287-
selCallback.eventCallback.call(selCallback.eventTarget, this);
1287+
cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
12881288
}
12891289
locListeners.length = 0;
12901290
}

cocos2d/core/textures/CCTextureCache.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ cc.TextureCache = cc.Class.extend(/** @lends cc.TextureCache# */{
9797
},
9898

9999
_addImageAsyncCallBack:function (target, selector) {
100-
if (target && (typeof(selector) === "string")) {
101-
target[selector]();
102-
} else if (target && (typeof(selector) === "function")) {
103-
selector.call(target);
104-
}
100+
cc.doCallback(selector, target);
105101
},
106102

107103
_initializingRenderer : function(){

cocos2d/label_nodes/CCLabelAtlas.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{
6868
var locListeners = this._loadedEventListeners;
6969
for(var i = 0, len = locListeners.length; i < len; i++){
7070
var selCallback = locListeners[i];
71-
selCallback.eventCallback.call(selCallback.eventTarget, this);
71+
cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
7272
}
7373
locListeners.length = 0;
7474
},

cocos2d/label_nodes/CCLabelBMFont.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{
522522
var locListeners = this._loadedEventListeners;
523523
for(var i = 0, len = locListeners.length; i < len; i++){
524524
var selCallback = locListeners[i];
525-
selCallback.eventCallback.call(selCallback.eventTarget, this);
525+
cc.doCallback(selCallback.eventCallback, selCallback.eventTarget, this);
526526
}
527527
locListeners.length = 0;
528528
},

cocos2d/menu_nodes/CCMenuItem.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,7 @@ cc.MenuItem = cc.NodeRGBA.extend(/** @lends cc.MenuItem# */{
184184
*/
185185
activate:function () {
186186
if (this._isEnabled) {
187-
var locTarget = this._target, locCallback = this._callback;
188-
if(!locCallback)
189-
return ;
190-
if (locTarget && (typeof(locCallback) == "string")) {
191-
locTarget[locCallback](this);
192-
} else if (locTarget && (typeof(locCallback) == "function")) {
193-
locCallback.call(locTarget, this);
194-
} else
195-
locCallback(this);
187+
cc.doCallback(this._callback, this._target,this);
196188
}
197189
}
198190
});

samples

0 commit comments

Comments
 (0)