BobDylan Posted September 10, 2014 Share Posted September 10, 2014 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 More sharing options...
eguneys Posted September 10, 2014 Share Posted September 10, 2014 try game.load.onLoadComplete. Link to comment Share on other sites More sharing options...
BobDylan Posted September 10, 2014 Author Share Posted September 10, 2014 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 More sharing options...
eguneys Posted September 10, 2014 Share Posted September 10, 2014 no the docs say, it fires when the last file is downloaded, so it fires only once on the last time. So use it like this:game.load.onFileComplete.add(function() { var data = game.cache.getJSON('test'); console.log(data);}, game); Link to comment Share on other sites More sharing options...
BobDylan Posted September 11, 2014 Author Share Posted September 11, 2014 Somehow the event is fired multiple times, when I have multiple load's in the preload.I'm using different gamestates now, which is the supposed way to load assets in multiple stages. Link to comment Share on other sites More sharing options...
jonoco Posted April 10, 2015 Share Posted April 10, 2015 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:onLoadCompletewhich will dispatch once after all the files are finished loading. Link to comment Share on other sites More sharing options...
Recommended Posts