craftyjs.github.com

Crafty.loader

public void Crafty.load(Array assets, Function onLoad[, Function onProgress, Function onError])

assets
Array of assets to load (accepts sounds and images)
onLoad
Callback when the assets are loaded
onProgress
Callback when an asset is loaded. Contains information about assets loaded
onError
Callback when an asset fails to load

Preloader for all assets. Takes an array of URLs and adds them to the Crafty.assets object.

Files with suffixes in image_whitelist (case insensitive) will be loaded.

If Crafty.support.audio is true, files with the following suffixes mp3, wav, ogg and mp4 (case insensitive) can be loaded.

The onProgress function will be passed on object with information about the progress including how many assets loaded, total of all the assets to load and a percentage of the progress.

{ loaded: j, total: total, percent: (j / total * 100) ,src:src})

onError will be passed with the asset that couldn't load.

When onError is not provided, the onLoad is loaded even some assets are not successfully loaded. Otherwise, onLoad will be called no matter whether there are errors or not.

Example

Crafty.load(["images/sprite.png", "sounds/jump.mp3"],
    function() {
        //when loaded
        Crafty.scene("main"); //go to main scene
        Crafty.audio.play("jump.mp3"); //Play the audio file
    },

    function(e) {
      //progress
    },

    function(e) {
      //uh oh, error loading
    }
);

See Also

See Also