@@ -63,11 +63,10 @@ cc.ResData = function (resList, selector, target) {
63
63
cc . Loader = cc . Class . extend ( /** @lends cc.Loader# */ {
64
64
_curData : null ,
65
65
_resQueue : null ,
66
- _animationInterval : 1 / 60 ,
67
- _interval : null ,
68
66
_isAsync : false ,
69
67
_scheduler : null ,
70
68
_running : false ,
69
+ _regisiterLoader : false ,
71
70
72
71
/**
73
72
* Constructor
@@ -88,15 +87,35 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
88
87
cc . log ( "cocos2d:resources should not null" ) ;
89
88
return ;
90
89
}
90
+ var res = resources . concat ( [ ] ) ;
91
+ this . _resQueue . push ( new cc . ResData ( res , selector , target ) ) ;
91
92
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
+ }
94
98
} ,
95
99
96
100
setAsync : function ( isAsync ) {
97
101
this . _isAsync = isAsync ;
98
102
} ,
99
103
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
+
100
119
/**
101
120
* Callback when a resource file load failed.
102
121
* @example
@@ -127,12 +146,10 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
127
146
*/
128
147
getPercentage : function ( ) {
129
148
var percent = 0 , curData = this . _curData ;
130
- if ( ! curData ) {
131
- percent = 0 ;
132
- }
133
- else if ( curData . totalNumber == 0 ) {
149
+ if ( curData . totalNumber == 0 ) {
134
150
percent = 100 ;
135
- } else {
151
+ }
152
+ else {
136
153
percent = ( 0 | ( curData . loadedNumber / curData . totalNumber * 100 ) ) ;
137
154
}
138
155
return percent ;
@@ -183,8 +200,7 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
183
200
}
184
201
} ,
185
202
186
- _preload : function ( ) {
187
- this . _updatePercent ( ) ;
203
+ update : function ( ) {
188
204
if ( this . _isAsync ) {
189
205
var frameRate = cc . Director . getInstance ( ) . _frameRate ;
190
206
if ( frameRate != null && frameRate < 20 ) {
@@ -194,14 +210,25 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
194
210
}
195
211
196
212
var curData = this . _curData ;
197
- if ( ! curData && this . _resQueue . length > 0 ) {
198
- this . _curData = this . _resQueue . shift ( ) ;
199
- }
200
-
201
213
if ( curData && curData . curNumber < curData . totalNumber ) {
202
214
this . _loadRes ( ) ;
203
215
curData . curNumber ++ ;
204
216
}
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
+ }
205
232
} ,
206
233
207
234
_loadRes : function ( ) {
@@ -212,46 +239,35 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
212
239
213
240
var resInfo = this . _curData . resList . shift ( ) ,
214
241
path = this . _getResPath ( resInfo ) ,
215
- type = this . _getResType ( resInfo , path ) ;
242
+ type = this . _getResType ( resInfo , path ) ,
243
+ cb = this . onResLoaded . bind ( this ) ;
216
244
217
245
switch ( type ) {
218
246
case "IMAGE" :
219
- sharedTextureCache . addImage ( path ) ;
247
+ sharedTextureCache . addImageAsync ( path , cb ) ;
220
248
break ;
221
249
case "SOUND" :
222
250
if ( ! sharedEngine ) throw "Can not find AudioEngine! Install it, please." ;
223
- sharedEngine . preloadSound ( path ) ;
251
+ sharedEngine . preloadSound ( path , cb ) ;
224
252
break ;
225
253
case "XML" :
226
- sharedParser . preloadPlist ( path ) ;
254
+ sharedParser . preloadPlist ( path , cb ) ;
227
255
break ;
228
256
case "BINARY" :
229
- sharedFileUtils . preloadBinaryFileData ( path ) ;
257
+ sharedFileUtils . preloadBinaryFileData ( path , cb ) ;
230
258
break ;
231
259
case "TEXT" :
232
- sharedFileUtils . preloadTextFileData ( path ) ;
260
+ sharedFileUtils . preloadTextFileData ( path , cb ) ;
233
261
break ;
234
262
case "FONT" :
235
- this . _registerFaceFont ( resInfo ) ;
263
+ this . _registerFaceFont ( resInfo , cb ) ;
236
264
break ;
237
265
default :
238
266
throw "cocos2d:unknown filename extension: " + type ;
239
267
break ;
240
268
}
241
269
} ,
242
270
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
-
255
271
_getResPath : function ( resInfo ) {
256
272
return typeof resInfo == "string" ? resInfo : resInfo . src ;
257
273
} ,
@@ -276,24 +292,8 @@ cc.Loader = cc.Class.extend(/** @lends cc.Loader# */{
276
292
}
277
293
} ,
278
294
279
- _updatePercent : function ( ) {
280
- var percent = this . getPercentage ( ) ;
281
-
282
- if ( percent >= 100 ) {
283
- this . _unschedulePreload ( ) ;
284
- this . _complete ( ) ;
285
- }
286
- } ,
287
-
288
295
_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 ) ;
297
297
298
298
this . _curData = null ;
299
299
if ( this . _resQueue . length > 0 ) {
@@ -398,6 +398,12 @@ cc.Loader.purgeCachedData = function (resources) {
398
398
}
399
399
} ;
400
400
401
+ cc . Loader . register = function ( typeArr , loader ) {
402
+ if ( this . _instance ) {
403
+ this . _instance . registerWithType ( typeArr , loader ) ;
404
+ }
405
+ } ;
406
+
401
407
/**
402
408
* Returns a shared instance of the loader
403
409
* @function
0 commit comments