Jump to content

Read from .json file(TypeScript)


klam A.
 Share

Recommended Posts

Hi. English is not my native language, so sory for my mistakes. I make game in VisualStudio(TypeScript), and create 

levels in Tiled Map Editor. So my questions is: how I can read information in my TypeSript game from level.json file?

Thanks.

Link to comment
Share on other sites

 

Simplistically speaking, you could load it with an AJAX call and parse the JSON:

function levelRequestListener () {

var levels = JSON.parse(this.responseText);

console.log(levels);

}

var request = new XMLHttpRequest();

request.onload = levelRequestListener;

request.open("get", "level.json", true);

request.send();

You could take this up a level by writing an interface to describe the levels structure so you could get type checking and auto-completion on the levels variable...

interface Level {

id: number;

name: string;

}

function levelRequestListener () {

var levels: Level[] = JSON.parse(this.responseText);

console.log(levels[0].name);

}

 

Hope this helps.

 

Fow more applications design and development visit: http://www.ati-erp.com

 

Link to comment
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...
 Share

  • Recently Browsing   0 members

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