Jump to content

Phaser Json Read


rahulng
 Share

Recommended Posts

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 block

heres 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

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

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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