cykod / Quintus

HTML5 Game Engine
http://html5quintus.com
GNU General Public License v2.0
1.41k stars 401 forks source link

loading local files like json #106

Closed fraespre closed 10 years ago

fraespre commented 10 years ago

Hi,

I think it's very important than we can load local resources like the json files. This is significant if you want make a local game to run in a mobile.

I don't know what is exactly the problem, I suspect this is a safety restriction. But another JS game engine (similar to quintus) can load this type of files: https://github.com/Galloman/ss2d

I suggest than you can investigate how can resolve the problem watching the ss2d source code (ResourceManager.loadResources).

regards,

fariazz commented 10 years ago

if you are using Phonegap, it loads local files using it's own web server, so it should be able to load the files without problem. What are you using to wrap the game on mobile?

cykod commented 10 years ago

You can't load JSON files via AJAX from a file:// URL... You can easily turn your json file into a .js file with a Quintus module.

Quintus.MyData = function(Q) { Q.assets['asset_name'] = { ... } }

Then include that module in your setup.

fraespre commented 10 years ago

Sorry Cykod, but this solution seemed unserious (to insert json data hardcoded in js file).

I have developed a little function to load local files. Now I can execute fine the samples using local assets. You could append a similar utility in the Quintus to facilitate the local games.

//Q.load("sprites.png, sprites.json, level.json, tiles.png, background-wall.png", function() { Q.load("sprites.png, tiles.png, background-wall.png", function() { Q.assets['sprites.json']= loadJson("./data/sprites.json"); Q.assets['level.json']= loadJson("./data/level.json"); ... function loadJson(filePath) { var req, res; req= new XMLHttpRequest(); req.open("GET", filePath, false); req.onload= function(){ res= this.response; }; req.send(null); res= ((res!=undefined)?JSON.parse(res):null); return res; }

jerone commented 10 years ago

This won't work for everyone and on every browser. For security reasons JavaScript's access to the file system on the client is restricted - consider whether you would want (somebody else's) JavaScript to read your sensitive documents. Someone can disable that "security feature" in his browser manually. For instance, for Google Chrome you have to startup the executable with --disabled-web-security as commandline argument. Firefox can disabled that within it's about:config.

cykod commented 10 years ago

@fraespre - are you trolling? That's exactly what the current system does: https://github.com/cykod/Quintus/blob/master/lib/quintus.js#L1695

See @jerone's comment about security.

fraespre commented 10 years ago

@cykod , I was interested and downloaded the latest version of Quintus to Local. I probed the different examples and I found most samples crashed when load json files. [ Win 7 &Firefox 28.0 ] > uncaught exception: Error Loading: sprites.json ... I notify you with this issue. You say me than this is a normal comportament. I search alternative to resolve the problem than work fine (you can probe it).

Do you think this is the troll behaviour?