Cocos Creator API

1.3.0

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

loader static

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

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 Pipeline/onProgress:method The complete callback is almost the same as Pipeline's Pipeline/onComplete:method 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 of (project/assets/resources/imgs/cocos.png) 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 the corresponding sprite frame
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
)
Any

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

:

: Any

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.

setAutoRelease
(
  • assetOrUrlOrUuid
  • autoRelease
)

Indicates whether to release the asset when loading a new scene.
By default, when loading a new scene, all assets in the previous scene will be released or preserved according to whether the previous scene checked the "Auto Release Assets" option. On the other hand, assets dynamically loaded by using cc.loader.loadRes or cc.loader.loadResAll will not be affected by that option, remain not released by default.
Use this API to change the default behavior on a single asset, to force preserve or release specified asset when scene switching.

See: cc.loader.setAutoReleaseRecursively, cc.loader.isAutoRelease

assetOrUrlOrUuid Asset | String

asset object or the raw asset's url or uuid

autoRelease Boolean

indicates whether should release automatically

:

// auto release the texture event if "Auto Release Assets" disabled in current scene
cc.loader.setAutoRelease(texture2d, true);
// don't release the texture even if "Auto Release Assets" enabled in current scene
cc.loader.setAutoRelease(texture2d, false);
// first parameter can be url
cc.loader.setAutoRelease(audioUrl, false);

setAutoReleaseRecursively
(
  • assetOrUrlOrUuid
  • autoRelease
)

Indicates whether to release the asset and its referenced other assets when loading a new scene.
By default, when loading a new scene, all assets in the previous scene will be released or preserved according to whether the previous scene checked the "Auto Release Assets" option. On the other hand, assets dynamically loaded by using cc.loader.loadRes or cc.loader.loadResAll will not be affected by that option, remain not released by default.
Use this API to change the default behavior on the specified asset and its recursively referenced assets, to force preserve or release specified asset when scene switching.

See: cc.loader.setAutoRelease, cc.loader.isAutoRelease

assetOrUrlOrUuid Asset | String

asset object or the raw asset's url or uuid

autoRelease Boolean

indicates whether should release automatically

:

// auto release the SpriteFrame and its Texture event if "Auto Release Assets" disabled in current scene
cc.loader.setAutoReleaseRecursively(spriteFrame, true);
// don't release the SpriteFrame and its Texture even if "Auto Release Assets" enabled in current scene
cc.loader.setAutoReleaseRecursively(spriteFrame, false);
// don't release the Prefab and all the referenced assets
cc.loader.setAutoReleaseRecursively(prefab, false);

isAutoRelease
(
  • assetOrUrl
)
Boolean

Returns whether the asset is configured as auto released, despite how "Auto Release Assets" property is set on scene asset.

See: cc.loader.setAutoRelease, cc.loader.setAutoReleaseRecursively

assetOrUrl Asset | String

asset object or the raw asset's url

:

Pipeline
(
  • pipes
)

Constructor, pass an array of pipes to construct a new Pipeline, the pipes will be chained in the given order.
A pipe is an object which must contain an id in string and a handle function, the id must be unique in the pipeline.
It can also include async property to identify whether it's an asynchronous process.

pipes Array

:

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

insertPipe
(
  • pipe
  • index
)

Insert a new pipe at the given index of the pipeline.
A pipe must contain an id in string and a handle function, the id must be unique in the pipeline.

pipe Object

The pipe to be inserted

index Number

The index to insert

appendPipe
(
  • pipe
)

Add a new pipe at the end of the pipeline.
A pipe must contain an id in string and a handle function, the id must be unique in the pipeline.

pipe Object

The pipe to be appended

flowIn
(
  • items
)

Let new items flow into the pipeline.
Each item can be a simple url string or an object, if it's an object, it must contain id property.
You can specify its type by type property, by default, the type is the extension name in url.
By adding a skips property including pipe ids, you can skip these pipe.
The object can contain any supplementary property as you want.

items Array

:

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

flowInDeps
(
  • urlList
  • callback
)
Array deprecated

Pipeline:

: since v1.3

Let new items flow into the pipeline and give a callback when the list of items are all completed.
This is for loading dependencies for an existing item in flow, usually used in a pipe logic.
For example, we have a loader for scene configuration file in JSON, the scene will only be fully loaded
after all its dependencies are loaded, then you will need to use function to flow in all dependencies
found in the configuration file, and finish the loader pipe only after all dependencies are loaded (in the callback).

urlList Array
callback Function

:

: Array

Items accepted by the pipeline

copyItemStates
(
  • srcItem
  • dstItems
)

Copy the item states from one source item to all destination items.
It's quite useful when a pipe generate new items from one source item,
then you should flowIn these generated items into pipeline,
but you probably want them to skip all pipes the source item already go through,
you can achieve it with this API.

For example, an unzip pipe will generate more items, but you won't want them to pass unzip or download pipe again.

srcItem Object

The source item

dstItems Array | Object

A single destination item or an array of destination items

isFlowing ( ) Boolean deprecated

Pipeline:

: since v1.3

Returns whether the pipeline is flowing (contains item) currently.

:

getItems ( ) LoadingItems deprecated

Pipeline:

: since v1.3

Returns all items in pipeline.

getItem
(
  • id
)
Object

Returns an item in pipeline.

id Object

The id of the item

:

: Object

removeItem
(
  • id
)
Boolean

Removes an item in pipeline, no matter it's in progress or completed.

id Object

The id of the item

:

: Boolean

succeed or not

clear ( )

Clear the current pipeline, this function will clean up the 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