构建一个瓷砖游戏如何从外部加载关卡数据
我正在构建一个包含 3 层“图块”的基本图块游戏。
- 图块本身的
- 对象
- 项目
图像游戏的尺寸为 3x3 我的数据数组如下所示:
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,],
];
我有 2 个问题:
- 我如何从关卡设计师可以轻松编辑的外部文件吗? (最好使用什么,xml,json,...?)
- 仅使用 1 个数据文件而不是 3 个数据文件不是更好吗?最好的方法是什么?
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 use 1 datafile instead of 3 and what is the best way to do this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我的意见。
每个加载文件操作都在操作字符串(字符集),因此使用纯字符串比任何其他格式更好。对于 XML,Actionscript 有内置函数将读取的字符串解析为 XML 类型的变量。用于从字符串解析 JSON 格式的库也可以在互联网上的某个地方找到。但两者都需要额外的资源。
对于您的情况,我建议如下解决方案:
_ 配置文件存储如下字符串:
_ 在您的应用程序中,您需要两个步骤来解析配置文件:
+ 通过使用换行符拆分文件数据,将文件解析为一组级别
再次拆分每个级别以接收实际数组
var level1:Array =levels[0].split(",");
希望这有帮助。
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:
_ 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
split each level again to receive actual array
var level1:Array = levels[0].split(",");
Hope this help.
1)我认为最好使用 CSV(逗号分隔值)格式来指定二维数组。例如,指定
tiles_Array
的tiles_array.csv
文件将类似于以下内容:2) 恕我直言,关卡设计师拥有 3 个单独的文件会更方便
1) I think it's better use CSV (Comma Separated Values) format for specifying 2d array. E.g.
tiles_array.csv
file specifyingtiles_Array
will be something like the following:2) IMHO, it'll be more convenient for level designers to have 3 separate files
对于关卡数据,您列出的任何文件格式都可以使用。我经常使用 XML,但这主要是因为 Flash 有内置命令来处理 XML,因此解析起来稍微方便一些。 JSON 也可以通过免费提供的库正常工作,或者您可以使用自己的文件格式并使用简单的字符串命令对其进行解析。最后,只要您选择的文件格式足够灵活以适应未来不可避免的扩展(例如,您已经向游戏中添加了新类型的实体),这并不重要。
将单独的数据存储为单独的文件几乎总是更好,因为这为您提供了如何管理文件的更大灵活性。我能想到的一个例外是,如果您想将关卡数据存储在数据库中,这是一种非常罕见的情况。我个人从未这样做过,但我知道这对于具有用户生成级别的在线游戏来说是有意义的。
实际上加载文件是使用 URLLoader 完成的,如下所述:http://www.kirupa。 com/developer/flashcs3/using_xml_as3_pg2.htm
(注意:这仅适用于单独的平面文件。存储在数据库中的级别需要完全不同的工作流程,涉及对服务器上的后端代码发出 GET 请求。)
为了编辑数据,关卡设计者永远不应该直接查看 XML(或 JSON 或 CSV 或其他)数据,而应编写一个关卡编辑器使用它们,并且该编辑器将具有加载和保存关卡的命令。如果您使用 Air,那么您可以直接保存文本文件,但即使没有,您也可以简单地让编辑器将数据转储到文本字段,然后您可以将其粘贴到文件中。
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.