Jump to content

Synchronous loading with game.load.json() ?


Recommended Posts

In 'preload' I'm using

 game.load.json('test', 'test.json');

to load some data. This starts an asynchronous request, so I can only use the results in 'create' :

var jsoncontents = game.cache.getJSON('test');

But now I want to load some other json, based on the contents of the first json file.


Somehow I need a synchronous request. With jQuery you can specify if an ajax-request should be asynchronous or not.

Any way to achieve the same in Phaser?

Link to comment
Share on other sites

I already tried onFileComplete and jsonLoadComplete but couldn't find any examples how to use these.

Somehow onFileComplete is not triggered, or I'm doing something wrong.


This is NOT working (no alert is shown) :

      game.load.onFileComplete.add(function(key) {        if (key === 'test') {          var data = game.cache.getJSON(key);          alert(data);        }      }, game);

How would I use onLoadComplete ?

Link to comment
Share on other sites

  • 6 months later...

In case anyone is looking at this for reference later:

onFileCompleteParams: (progress, file key, success?, total loaded files, total files)

is dispatched each time a file has finished loading, that means if you're loading 5 files, you'll get the event 5 times. 


This is good if you want to watch the loading progress, but if you are watching for particular files loading in, be sure you grab the 2nd parameter, which will hold the key. Add this to the end of your preload function to get an idea how it works.

game.load.onFileComplete.add(function(progress, key) {  console.log(progress + '%');  console.log(key + ' loaded');}, game);

If however you just want to know when all files are loaded, use:


which will dispatch once after all the files are finished loading.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...