开发者

Building a tile game how to load the level data externally

I'm building a basic tile game containing 3 layers of 'tiles'

  • The tiles itself
  • Objects
  • Items

image the game has a dimension of 3x3 my data array's look like this:

    public var tiles_Array:Array = 
    [   [1, 1, 1,],
        [1, 2, 1,], 
        [1, 1, 1,], 
    ];

    public var objects_Array:Array = 
    [   [1, 3, 1,],
        [5, 4, 1,], 
        [1, 7, 1,], 
    ];

    public var items_Array:Array = 
    [   [1, 1, 1,],
        [1, 1, 8,], 
        [5, 1, 1,], 
    ];

I've got 2 questions:

  • How can i load this data from an external file which is easy to edit for the level-desiners ? (and what is best to use, xml, json, ... ?)
  • Is is not better to just开发者_如何学JAVA use 1 datafile instead of 3 and what is the best way to do this ?


here is my opinion.

Every loading file action is manipulating the string (set of characters), therefore using pure string is better than any other format. For XML, Actionscript has built-in function to parse read strings into XML-typed variable. Library for parsing JSON format from string is also available somewhere on the internet. But both of them cost extra resource.

For your case, I propose a solution like below:

_ The config file stores string like this:

1,1,1,1,2,1,1,1,1
1,3,1,5,4,1,1,7,1
1,1,1,1,1,8,5,1,1

_ In your application, you need two steps to parse the config file: + Parse file into set of levels by splitting file data with line break characters

var levels:Array = fileData.split("\n");
  • split each level again to receive actual array

    var level1:Array = levels[0].split(",");

Hope this help.


1) I think it's better use CSV (Comma Separated Values) format for specifying 2d array. E.g. tiles_array.csv file specifying tiles_Array will be something like the following:

1,2,1
1,2,1
1,1,1

2) IMHO, it'll be more convenient for level designers to have 3 separate files


For the level data, any of the file formats you listed could work. I often use XML, but that's mostly because Flash has built-in commands to handle XML so that's slightly more convenient to parse. JSON also works fine through freely available libraries, or you could just roll your own file format and parse it using simple string commands. In the end this doesn't really matter so long as your chosen file format is flexible enough to accommodate the inevitable future extensions (eg. you've added new types of entities to your game).

It is almost always better to store separate data as separate files because that gives you much more flexibility for how to manage the files. The one exception I can think of would be if you want to store the level data in a database, and that's a pretty rare situation. I've never done that personally, but I know that would make sense for an online game with user generated levels.

Actually loading the file is done using a URLLoader as described here: http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg2.htm

(Note: That only applies to separate flat-files. Levels stored in a database would require a completely different workflow, involving GET requests made to backend code on your server.)

For editing the data, the level designers should never be looking at the XML (or JSON or CSV or whatever) data directly and instead you would write a level editor for them to use, and that editor would have commands to load and save levels. If you're using Air then you can save text files directly, but even without that you could simply have the editor dump out the data to a text field that you can paste into a file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜