Cocos Creator API

1.4.x

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

loader static

Extends Pipeline
Module: 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

Methods

addDownloadHandlers
(
  • extMap
)

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

name type description
extMap Object

Custom supported types with corresponded handler

examples:

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.

name type description
extMap Object

Custom supported types with corresponded handler

examples:

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.

name type description
resources String | Array

Url list in an array

progressCallback optional Function

Callback invoked when progression change

completeCallback Function

Callback invoked when all resources loaded

examples:

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({url: '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.

name type description
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.

examples:

// 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));
});