actionscript-3flixel

Embedding a file with a variable in AS3 + flixel


I have recently picked up flixel (I have programmed before, but I have not in a while) and I have come across a problem. I am attempting to create maps, and eventually there will be multiple maps available.

I currently have a .txt file that has information that eventually goes into an array. Then I go from array to map with loadmap. It is maybe a simple way to accomplish this task, and maybe their are better ways (I have not explored all the possibilities with flixel, and if there are any opinions, go ahead and let me know) but it works good for now.

As I have previously said, I am trying to do this with multiple maps. I could do this by using [Embed(source = "")] for each .txt file, but this may end up being annoying. So, here is my question: Is there a possible way Embed a file based upon a variable?

My Map class looks like this:

public function Map(MapSet:String, TileSet:String) 
        {
            super(MapSet, TileSet);
            //more stuff
        }

Now I have tried:

[Embed(scource="data/MapSets/" + MapSet + ".txt",  mimeType = "application/octet-stream")]private var loadedMap:Class

and then I use:

map = new Map("Map1x1", "ForestTiles");
add(map);

Is there a possibility of doing this in a different way? Or maybe I am doing something wrong? All opinions are welcome.


Solution

  • It's beneficial to know what code does when using it.

    Embed is a meta tag. It tells the compiler to include a certain file into the .swf file. That means this does not happen at runtime.

    When this embed code is "executed", your variables don't even exist yet. That's why your code cannot work.

    Despite not working, your solution is still valid: If you find it tedious to generate code, write a program that does this for you. Create/use a program that finds all valid files in the given directory and creates all the embed tags. Run this program before the compiler.