rahulng

Phaser Json Read

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

 

Share this post


Link to post
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;

}

Share this post


Link to post
Share on other sites

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 '

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.