简体   繁体   中英

Best way to store XML data for a game in AS3

I'm writing a game for AS3 that pulls in data from XML files, parses it and organises it into different Objects (custom AS3 Classes). It starts to get a bit tricky when I try and think of how I can store the image data, and how I can initialise it later on. At the moment I store the character's Display Object inside a custom Character sprite class. However, I thought it might actually be better to have a giant AssetLoader Class, and instead of storing the data inside a Character class, I store it one large array in the AssetLoader Class instead.

Which would be the most efficient way of doing this - considering that there could potentially be hundreds of character assets.

If possible, having assets organized per level is generally a good idea. Depending on the size of the level, you might be able to achieve even finer granularity ( eg: levels are divided into areas; let's say the player starts in area A, so you load all assets for area A; now let's say he travels from A to B via some connection door; you remove all area A assets and objects from memory - you prepare them for garbage collection - and you now load assets for area B - you can have a small loading message or something to signal the transition, think of games like Diablo that have a similar approach to level loading and area navigation ).

Whether you need such fine grained loading or not is hard to say without more detail. I recommend you start with the simpler approach ( assets per level ) and improve from there on if really needed. Remember: "Premature optimization is the root of all evil."

PS: You shouldn't organize just image data per level, ideally, you should organize all level information like that. If there's no need to display level-specific information before actually getting to the said level, then pre-loading it is a waste.

PPS: Depending on where the game will run and the size of it, you might want to consider ditching the XML approach and using a database ( eg: SQLite for desktop and/or MySQL for the web ) for storing game related data ( including images ). XML tends to fall short fast and it can easily get quite cumbersome to maintain, not to mention that you might eventually want to add new features that could really benefit from a centralized DB ( eg: scoreboards, user statistics, achievements, etc. ).

Storing image data in XML is probably not a good idea. Store the image data separately in a binary format and only store IDs or URLs in the XML. Personally, I prefer storing all object data in AMF format, which makes encoding and decoding easy and very fast.

我正在使用此静态类在100多个游戏类之间共享大型配置XML,一些运行时变量和一些对象: https : //github.com/inruntime/AS3-Global-Object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM