Cocos Creator API

1.2.0

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

loader

继承于 Pipeline
模块: cc

Loader for resource loading process. It's a singleton object.

属性(properties)

downloader Object

The downloader in cc.loader's pipeline, it's by default the first pipe. It's used to download files with several handlers: pure text, image, script, audio, font, uuid. You can add your own download function with addDownloadHandlers

loader Object

The downloader in cc.loader's pipeline, it's by default the second pipe. It's used to parse downloaded content with several handlers: JSON, image, plist, fnt, uuid. You can add your own download function with addLoadHandlers

There are no properties that match your current filter settings. You can change your filter settings in the index section on this page. index

方法

addDownloadHandlers
(
  • extMap
)

Add custom supported types handler or modify existing type handler for download process.

名称 类型 描述
extMap Object

Custom supported types with corresponded handler

示例:

cc.loader.addDownloadHandlers({
     // This will match all url with .scene extension or all url with scene type
     'scene' : function (url, callback) {}
 });

addLoadHandlers
(
  • extMap
)

Add custom supported types handler or modify existing type handler for load process.

名称 类型 描述
extMap Object

Custom supported types with corresponded handler

示例:

cc.loader.addLoadHandlers({
     // This will match all url with .scene extension or all url with scene type
     'scene' : function (url, callback) {}
 });

load
(
  • resources
  • [progressCallback ]
  • completeCallback
)

Load resources with a progression callback and a complete callback. The progression callback is the same as Pipeline's onProgress The complete callback is almost the same as Pipeline's onComplete The only difference is when user pass a single url as resources, the complete callback will set its result directly as the second parameter.

名称 类型 描述
resources String | Array

Url list in an array

progressCallback optional Function

Callback invoked when progression change

completeCallback Function

Callback invoked when all resources loaded

示例:

cc.loader.load('a.png', function (err, tex) {
    cc.log('Result should be a texture: ' + (tex instanceof cc.Texture2D));
});

cc.loader.load('http://example.com/a.png', function (err, tex) {
    cc.log('Should load a texture from external url: ' + (tex instanceof cc.Texture2D));
});

cc.loader.load({id: 'http://example.com/getImageREST?file=a.png', type: 'png'}, function (err, tex) {
    cc.log('Should load a texture from RESTful API by specify the type: ' + (tex instanceof cc.Texture2D));
});

cc.loader.load(['a.png', 'b.json'], function (errors, results) {
    if (errors) {
        for (var i = 0; i < errors.length; i++) {
            cc.log('Error url [' + errors[i] + ']: ' + results.getError(errors[i]));
        }
    }
    var aTex = results.getContent('a.png');
    var bJsonObj = results.getContent('b.json');
});

loadRes
(
  • url
  • [type ]
  • completeCallback
)

Load resources from the "resources" folder inside the "assets" folder of your project.

Note: All asset urls in Creator use forward slashes, urls using backslashes will not work.

名称 类型 描述
url String

Url of the target resource. The url is relative to the "resources" folder, extensions must be omitted.

type optional Function

Only asset of type will be loaded if this argument is supplied.

completeCallback Function

Callback invoked when the resource loaded.

  • error Error

    The error info or null if loaded successfully.

  • resource Object

    The loaded resource if it can be found otherwise returns null.

示例:

// load the prefab (project/assets/resources/misc/character/cocos) from resources folder
cc.loader.loadRes('misc/character/cocos', function (err, prefab) {
    if (err) {
        cc.error(err.message || err);
        return;
    }
    cc.log('Result should be a prefab: ' + (prefab instanceof cc.Prefab));
});

// load the sprite frame (project/assets/resources/imgs/cocos.png/cocos) from resources folder
cc.loader.loadRes('imgs/cocos', cc.SpriteFrame, function (err, spriteFrame) {
    if (err) {
        cc.error(err.message || err);
        return;
    }
    cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame));
});

loadResAll
(
  • url
  • [type ]
  • completeCallback
)

Load all assets in a folder inside the "assets/resources" folder of your project.

Note: All asset urls in Creator use forward slashes, urls using backslashes will not work.

名称 类型 描述
url String

Url of the target folder. The url is relative to the "resources" folder, extensions must be omitted.

type optional Function

Only asset of type will be loaded if this argument is supplied.

completeCallback Function

A callback which is called when all assets have been loaded, or an error occurs.

  • error Error

    If one of the asset failed, the complete callback is immediately called with the error. If all assets are loaded successfully, error will be null.

  • assets Object[]

    An array of all loaded assets. If nothing to load, assets will be an empty array. If error occurs, assets will be null.

示例:

// load the texture (resources/imgs/cocos.png) and sprite frame (resources/imgs/cocos.png/cocos)
cc.loader.loadResAll('imgs/cocos', function (err, assets) {
    if (err) {
        cc.error(err);
        return;
    }
    var texture = assets[0];
    var spriteFrame = assets[1];
});

// load all textures in "resources/imgs/"
cc.loader.loadResAll('imgs', cc.Texture2D, function (err, textures) {
    if (err) {
        cc.error(err);
        return;
    }
    var texture1 = textures[0];
    var texture2 = textures[1];
});

getRes
(
  • url
)

Get resource data by id.
When you load resources with load or loadRes, the url will be the unique identity of the resource. After loaded, you can acquire them by passing the url to this API.

名称 类型 描述
url String

返回:

类型:

getItem ( ) LoadingItem

Returns an item in pipeline.

返回:

类型: LoadingItem

release
(
  • url
)

Release the cache of resource by url.

名称 类型 描述
url String

releaseAsset
(
  • asset
)

Release the loaded cache of asset.

名称 类型 描述
asset Asset

releaseRes
(
  • url
)

Release the cache of resource which loaded by loadRes.

名称 类型 描述
url String

releaseAll ( )

Resource cache of all resources.

Pipeline
(
  • pipes
)

继承自 Pipeline:

构造函数,通过一系列的 pipe 来构造一个新的 pipeline,pipes 将会在给定的顺序中被锁定。
一个 pipe 就是一个对象,它包含了字符串类型的 ‘id’ 和 ‘handle’ 函数,在 pipeline 中 id 必须是唯一的。
它还可以包括 ‘async’ 属性以确定它是否是一个异步过程。

名称 类型 描述
pipes Array

示例:

var pipeline = new Pipeline([
     {
         id: 'Downloader',
         handle: function (item, callback) {},
         async: true
     },
     {id: 'Parser', handle: function (item) {}, async: false}
 ]);

insertPipe
(
  • pipe
  • index
)

继承自 Pipeline:

在给定的索引位置插入一个新的 pipe。
一个 pipe 必须包含一个字符串类型的 ‘id’ 和 ‘handle’ 函数,该 id 在 pipeline 必须是唯一标识。

名称 类型 描述
pipe Object

The pipe to be inserted

index Number

The index to insert

appendPipe
(
  • pipe
)

继承自 Pipeline:

添加一个新的 pipe 到 pipeline 尾部。
该 pipe 必须包含一个字符串类型 ‘id’ 和 ‘handle’ 函数,该 id 在 pipeline 必须是唯一标识。

名称 类型 描述
pipe Object

The pipe to be appended

flowIn
(
  • urlList
)
Array

继承自 Pipeline:

让新的 item 流入 pipeline 中。
这里的每个 item 可以是一个简单字符串类型的 url 或者是一个对象, 如果它是一个对象的话,他必须要包含 ‘id’ 属性。
你也可以指定它的 ‘type’ 属性类型,默认情况下,该类型是 ‘url’ 的后缀名。
也通过添加一个 包含 ‘skips’ 属性的 item 对象,你就可以跳过 skips 中包含的 pipe。
该对象可以包含任何附加属性。

名称 类型 描述
urlList Array

返回:

类型: Array

Items accepted by the pipeline

示例:

pipeline.flowIn([
     'res/Background.png',
     {
         id: 'res/scene.json',
         type: 'scene',
         name: 'scene',
         skips: ['Downloader']
     }
 ]);

flowInDeps
(
  • urlList
  • callback
)
Array

继承自 Pipeline:

让新 items 流入 pipeline 并且当 item 列表完成时进行回调函数。
这个 API 的使用通常是为了加载依赖项。
例如:
我们需要加载一个场景配置的 JSON 文件,该场景会将所有的依赖项全部都加载完毕以后,进行回调表示加载完毕。

名称 类型 描述
urlList Array
callback Function

返回:

类型: Array

Items accepted by the pipeline

copyItemStates
(
  • srcItem
  • dstItems
)

继承自 Pipeline:

从一个源 item 向所有目标 item 复制它的 pipe 状态,用于避免重复通过部分 pipe。
当一个源 item 生成了一系列新的 items 时很有用,
你希望让这些新的依赖项进入 pipeline,但是又不希望它们通过源 item 已经经过的 pipe,
但是你可能希望他们源 item 已经通过并跳过所有 pipes,
这个时候就可以使用这个 API。

名称 类型 描述
srcItem Object

The source item

dstItems Array | Object

A single destination item or an array of destination items

isFlowing ( ) Boolean

继承自 Pipeline:

获取 pipeline 当前是否正在处理中。

返回:

类型: Boolean

getItems ( ) LoadingItems

继承自 Pipeline:

获取 pipeline 中的所有 items。

返回:

类型: LoadingItems

removeItem ( ) Boolean

继承自 Pipeline:

移除指定 item,无论是进行时还是已完成。

返回:

类型: Boolean

succeed or not

clear ( )

继承自 Pipeline:

清空当前 pipeline,该函数将清理 items。

onProgress
(
  • completedCount
  • totalCount
  • item
)

继承自 Pipeline:

这个回调函数将在 item 流出 pipeline 时被调用,你应该重写该函数。

名称 类型 描述
completedCount Number

The number of the items that are already completed.

totalCount Number

The total number of the items.

item Object

The latest item which flow out the pipeline.

示例:

pipeline.onProgress = function (completedCount, totalCount, item) {
     var progress = (100 * completedCount / totalCount).toFixed(2);
     cc.log(progress + '%');
 }

onComplete
(
  • error
  • items
)

继承自 Pipeline:

该函数将在所有 item 流出 pipeline 时被调用,你应该重写该函数。

名称 类型 描述
error Array

All errored urls will be stored in this array, if no error happened, then it will be null

items LoadingItems

All items.

示例:

pipeline.onComplete = function (error, items) {
     if (error)
         cc.log('Completed with ' + error.length + ' errors');
     else
         cc.log('Completed ' + items.totalCount + ' items');
 }

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index