I'm writing a Flash game where the game levels are saved in small plain-text files, which I want to embed in the swf file. My current solution has a distinct code smell in its repetition, and I'm certain that there is a better way. My code is basically:
In a LevelLoader class, embed all the levels
[Embed( source="levels/1.dat", mimeType="application/octet-stream" )]
protected var level1:Class;
[Embed( source="levels/2.dat", mimeType="application/octet-stream" )]
protected var level2:Class;
When the level needs to be loaded, read the string:
var bytes:ByteArray;
if ( levelNumber == 1 ) {
bytes = new level1();
} else if ( levelNumber == 2 ) {
bytes = new level2();
}
var levelStr:String = bytes.readMultiByte( bytes.bytesAvailable, bytes.endian );
prepareLevel( levelStr );
There are a couple of problems with this approach:
- I have to add a line for embedding each level. Optimally, all files in a folder would be automatically embedded.
- A level can't be "prepared" from a string. I'd like to be able to pass LevelLoader a level number or level name as a string.
- I think that all strings are stored in memory rather than on disk.
How could I program this "correctly"? Is ActionScript 3 capable of solving this problem?