BIT-101 [2003-2017]

AS3 Embed and asset paths


I ran into a surprise last night as I was using the Embed metadata tag in an AS3 project. I had assumed that the path to the embedded asset would be relative to the project root. Turns out that it’s relative to the location of the individual .as file which does the embedding. After some thought, I see the logic of it, but it creates a few little problems… well maybe annoyances.

First, an example. Say you have a project dir that has a main class and a set of nested package folders:

Project
   Main.as
   com
      bit101
         projectname
            MyClass.as

Now, say MyClass.as embeds and uses icon.png. If you do not specify a path, it will look inside the projectname folder. I had been assuming that it would look in the main Project folder.

Now, obviously, you don’t want to start storing your assets in your classpath folders. Yuck! So, what are the alternatives.

  1. You can use an absolute path: C:/Documents and Settings/blah/blah/blah/icon.png

This makes the whole project completely unportable. So that’s no good.

  1. You can use relative paths to work your way back up the package chain til you get to the root: ../../../icon.png

I guess the latter is the only real workable solution, but I think it’s ugly. Say I decide to move MyClass into a new package, interface, that is part of projectname. In addition to any other refactoring, I have to adjust the path for any and all embeds, and count the little dots and mentally map them to package heirarchies. Just doesn’t seem very elegant. Too bad there is not a shortcut for this, say {projectroot}/icon.png for example.

Of course, for a larger application, you would probably create an asset manager class as a singleton that takes care of all the embedding and has getters that return the assets you need. But for smaller projects I guess we’re stuck with counting the dots.

« Previous Post
Next Post »