rahulng Posted February 7, 2014 Share Posted February 7, 2014 HI,Im a newbie to Phaser and I have a project goin on in which i simply want to read the contents(properties) of a Json file and display them.but I seem to have hit a road blockheres the JSON file { "frames": { "bird":{ "frame": { "x": 0, "y": 0, "w": 98, "h": 106 }, "sourceSize": { "w": "1024", "h": "768" } } }, "meta": { }} I want to access and display the the x: and y: values in the frame property.any help would be realy appreciated Link to comment Share on other sites More sharing options...
Nokdu Posted February 7, 2014 Share Posted February 7, 2014 read the file first and thenframes.bird.frame.x? Link to comment Share on other sites More sharing options...
rahulng Posted February 7, 2014 Author Share Posted February 7, 2014 Ive tried that here let me sho you the code game.load.atlasJSONHash('myJsonobj','bird.png','TestFile.json');//reading the JSOn filetext=frames.bird.frame.x; but this doesnt seem to work Link to comment Share on other sites More sharing options...
rich Posted February 7, 2014 Share Posted February 7, 2014 Are you trying to load a texture atlas? Or just parse some json? Link to comment Share on other sites More sharing options...
brejep Posted February 7, 2014 Share Posted February 7, 2014 For getting frame data from an atlas json file, I'd do something like: var game = new Phaser.Game(400, 400, Phaser.AUTO, '', { preload: preload, create: create }); function preload(game) { game.load.atlasJSONHash('myJsonobj','bird.png','TestFile.json');} function create(game) { var frameData = game.cache.getFrameData('myJsonobj'); var birdFrameX = frameData.getFrameByName('bird').x;} Or you could load in a JSON file that isn't anything to do with an atlas. To do that, I'd do something like: var game = new Phaser.Game(400, 400, Phaser.AUTO, '', { preload: preload, create: create }); function preload(game) { game.load.text('myJsonObj', 'TestFile.json');} function create(game) { var parsedJsonData = JSON.parse( game.cache.getText('myJsonObj') ); var birdFrameX = parsedJsonData.frames.bird.frame.x;} Link to comment Share on other sites More sharing options...
rahulng Posted February 17, 2014 Author Share Posted February 17, 2014 Thx guys.I got my solution by using a jquery function to read the json file using an ajax command Link to comment Share on other sites More sharing options...
KrishnaMv Posted February 18, 2014 Share Posted February 18, 2014 I tried the same thing as u said.., var hello = game.cache.getText('jsonText'); var jsonobj = JSON.parse( hello ); console.log(jsonobj.one); but im getting this error - Uncaught SyntaxError: Unexpected token ' Link to comment Share on other sites More sharing options...
Recommended Posts