title: How should I embed external data files for run-time retrieval in ActionScript 3? Architecture help needed
body: <p>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:</p>
  
  <p>In a LevelLoader class, embed all the levels</p>
  
  <pre><code>[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;
  </code></pre>
  
  <p>When the level needs to be loaded, read the string:</p>
  
  <pre><code>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 );
  </code></pre>
  
  <p>There are a couple of problems with this approach:</p>
  
  <ul>
  <li>I have to add a line for embedding each level. Optimally, all files in a folder would be automatically embedded.</li>
  <li>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.</li>
  <li>I think that all strings are stored in memory rather than on disk.</li>
  </ul>
  
  <p>How could I program this "correctly"? Is ActionScript 3 capable of solving this problem?</p>