LoadingItems 类型
继承于 CallbacksInvoker
模块: cc
LoadingItems 是一个加载对象队列,可以用来输送加载对象到加载管线中。
请不要直接使用 new 构造这个类的对象,你可以使用 LoadingItems.create 来创建一个新的加载队列,这样可以允许我们的内部对象池回收并重利用加载队列。
它有一个 map 属性用来存放加载项,在 map 对象中以 url 为 key 值。
每个对象都会包含下列属性:
- id:该对象的标识,通常与 url 相同。
- url:路径
- type: 类型,它这是默认的 URL 的扩展名,可以手动指定赋值。
- error:pipeline 中发生的错误将被保存在这个属性中。
- content: pipeline 中处理的临时结果,最终的结果也将被存储在这个属性中。
- complete:该标志表明该对象是否通过 pipeline 完成。
- states:该对象存储每个管道中对象经历的状态,状态可以是 Pipeline.ItemState.WORKING | Pipeline.ItemState.ERROR | Pipeline.ItemState.COMPLETE
对象可容纳其他自定义属性。
每个 LoadingItems 对象都会在 onComplete 回调之后被销毁,所以请不要持有它的引用并在结束回调之后依赖它的内容执行任何逻辑,有这种需求的话你可以提前复制它的内容。
索引
属性(properties)
mapObject存储所有加载项的对象。completedObject存储已经完成的加载项。totalCountNumber所有加载项的总数。completedCountNumber所有完成加载项的总数。activeBoolean是否启用。
方法
onProgress这个回调函数将在 item 加载结束后被调用。onComplete该函数将在加载队列全部完成时被调用。createLoadingItems 的构造函数,这种构造方式会重用内部对象缓冲池中的 LoadingItems 队列,以尽量避免对象创建。getQueue通过 item 对象获取它的 LoadingItems 队列。itemComplete通知 LoadingItems 队列一个 item 对象已完成,请不要调用这个函数,除非你知道自己在做什么。append向一个 LoadingItems 队列添加加载项。allComplete完成一个 LoadingItems 队列,请不要调用这个函数,除非你知道自己在做什么。isCompleted检查是否所有加载项都已经完成。isItemCompleted通过 id 检查指定加载项是否已经加载完成。exists通过 id 检查加载项是否存在。getContent通过 id 获取指定对象的内容。getError通过 id 获取指定对象的错误信息。addListener监听加载项(通过 key 指定)的完成事件。hasListener检查指定的加载项是否有完成事件监听器。remove移除指定加载项已经注册的完成事件监听器。removeAllListeners删除指定目标的所有完成事件监听器。itemComplete通知 LoadingItems 队列一个 item 对象已完成,请不要调用这个函数,除非你知道自己在做什么。destroy销毁一个 LoadingItems 队列,这个队列对象会被内部缓冲池回收,所以销毁后的所有内部信息都是不可依赖的。hasEventListener检查指定事件是否已注册回调。removeAll移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。off删除之前与同类型,回调,目标注册的回调。emit通过事件名发送自定义事件
Details
属性(properties)
map
存储所有加载项的对象。
| meta | description |
|---|---|
| 类型 | Object |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:212 |
completed
存储已经完成的加载项。
| meta | description |
|---|---|
| 类型 | Object |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:220 |
totalCount
所有加载项的总数。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:228 |
completedCount
所有完成加载项的总数。
| meta | description |
|---|---|
| 类型 | Number |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:236 |
active
是否启用。
| meta | description |
|---|---|
| 类型 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:244 |
方法
onProgress
这个回调函数将在 item 加载结束后被调用。你可以在构造时传递这个回调函数或者是在构造之后直接设置。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:179 |
参数列表
completedCountNumber The number of the items that are already completed.totalCountNumber The total number of the items.itemObject The latest item which flow out the pipeline.
示例
loadingItems.onProgress = function (completedCount, totalCount, item) {
var progress = (100 * completedCount / totalCount).toFixed(2);
cc.log(progress + '%');
}
onComplete
该函数将在加载队列全部完成时被调用。你可以在构造时传递这个回调函数或者是在构造之后直接设置。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:195 |
参数列表
errorsArray All errored urls will be stored in this array, if no error happened, then it will be nullitemsLoadingItems All items.
示例
loadingItems.onComplete = function (errors, items) {
if (error)
cc.log('Completed with ' + errors.length + ' errors');
else
cc.log('Completed ' + items.totalCount + ' items');
}
create
LoadingItems 的构造函数,这种构造方式会重用内部对象缓冲池中的 LoadingItems 队列,以尽量避免对象创建。 你可以传递 onProgress 和 onComplete 回调函数来获知加载进度信息。
| meta | description |
|---|---|
| 返回 | LoadingItems |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:292 |
参数列表
pipelinePipeline The pipeline to process the queue.urlListArray The items array.onProgressFunction The progression callback, refer to LoadingItems.onProgressonCompleteFunction The completion callback, refer to LoadingItems.onComplete
示例
cc.LoadingItems.create(cc.loader, ['a.png', 'b.plist'], function (completedCount, totalCount, item) {
var progress = (100 * completedCount / totalCount).toFixed(2);
cc.log(progress + '%');
}, function (errors, items) {
if (errors) {
for (var i = 0; i < errors.length; ++i) {
cc.log('Error url: ' + errors[i] + ', error: ' + items.getError(errors[i]));
}
}
else {
var result_a = items.getContent('a.png');
// ...
}
})
getQueue
通过 item 对象获取它的 LoadingItems 队列。
| meta | description |
|---|---|
| 返回 | LoadingItems |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:359 |
参数列表
itemObject The item to query
itemComplete
通知 LoadingItems 队列一个 item 对象已完成,请不要调用这个函数,除非你知道自己在做什么。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:371 |
参数列表
itemObject The item which has completed
append
向一个 LoadingItems 队列添加加载项。
| meta | description |
|---|---|
| 返回 | Array |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:439 |
参数列表
urlListArray The url list to be appended, the url can be object or string
allComplete
完成一个 LoadingItems 队列,请不要调用这个函数,除非你知道自己在做什么。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:523 |
isCompleted
检查是否所有加载项都已经完成。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:536 |
isItemCompleted
通过 id 检查指定加载项是否已经加载完成。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:546 |
参数列表
idString The item's id.
exists
通过 id 检查加载项是否存在。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:557 |
参数列表
idString The item's id.
getContent
通过 id 获取指定对象的内容。
| meta | description |
|---|---|
| 返回 | Object |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:568 |
参数列表
idString The item's id.
getError
通过 id 获取指定对象的错误信息。
| meta | description |
|---|---|
| 返回 | Object |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:590 |
参数列表
idString The item's id.
addListener
监听加载项(通过 key 指定)的完成事件。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:611 |
参数列表
hasListener
检查指定的加载项是否有完成事件监听器。 如果同时还指定了一个回调方法,并且回调有注册,它只会返回 true。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:622 |
参数列表
remove
移除指定加载项已经注册的完成事件监听器。 只会删除 key, callback, target 均匹配的监听器。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:637 |
参数列表
removeAllListeners
删除指定目标的所有完成事件监听器。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:652 |
参数列表
itemComplete
通知 LoadingItems 队列一个 item 对象已完成,请不要调用这个函数,除非你知道自己在做什么。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:684 |
参数列表
idString The item url
destroy
销毁一个 LoadingItems 队列,这个队列对象会被内部缓冲池回收,所以销毁后的所有内部信息都是不可依赖的。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/load-pipeline/loading-items.js:728 |
hasEventListener
检查指定事件是否已注册回调。
| meta | description |
|---|---|
| 返回 | Boolean |
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:188 |
参数列表
removeAll
移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:235 |
参数列表
off
删除之前与同类型,回调,目标注册的回调。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:279 |
参数列表
emit
通过事件名发送自定义事件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/platform/callbacks-invoker.js:309 |
参数列表
keyString event typearg1Any First argumentarg2Any Second argumentarg3Any Third argumentarg4Any Fourth argumentarg5Any Fifth argument
示例
eventTarget.emit('fire', event);
eventTarget.emit('fire', message, emitter);