Date display area
Dynamic Asset loading in Director
Dynamic assets in Director – an overview
A common commercial situation calls for a Director application to load some of its assets at runtime – either because they require a currency (news reports, venue schedules etc.) or, because the end-user should be able to add their own materials to a project. Director provides functions which are able to manipulate files, folders and casts to allow this. XML is often used to specify where the application should look for additional material).

Getting files from a folder
The following function is a simple example showing how to assemble a list of sound files in a folder. The folder in this example is called sounds and is in the same location as the host director movie (or projector in the production version). The list is then passed to a second function which actually imports the files into newly created cast members in the empty cast library called assets.
  1. Make a new director movie and create a new external cast called assets.

  2. Save your movie into a new folder called dynamic_loading. Call your movie dynamic_load_1.

  3. You will be asked for a filename for your new cast, call it assets and save it in the same folder.

  4. Open a movie script and then type the following: -
  5. function makeSoundList() { // gets list of sounds in sounds Folder _global.gsoundlist = list(); for(var i = 1; i<100;i++){ // files are in a folder called ‘sounds’ var fname = _movie.getNthFileNameInFolder("@/sounds", i); if (fname == "") { // no more sounds in folder - exit repeat break; } // don't load non-sound files // or system files if (fname.charAt(1) != ".") { if((fname.indexOf(".aif")!=-1)||(fname.indexOf(".wav")!=-1)){ // add sound files to sound list _global.gsoundList.add(fname); } } } } // this links sounds in gSoundList to new sound cast members function loadSounds() { // get the cast number var cNum = castLib("assets").number; // if there are some sounds if (_global.gSoundList.count > 0) { // go through each item for (var n = 1 ; n <= _global.gSoundList.count;n++) { // make a new cast member var memNum = _movie.newMember("sound",castLib(cNum)); // set its name member(memNum,cNum).name ="sound "+n; // link to the file member(memNum,cNum).filename="@/sounds/"+gSoundList[n]; } } }

  6. Make a new folder inside your dynamic_loading folder and call it sounds.

  7. Put 3 or 4 soundfiles inside this folder.

  8. To get them loaded into your cast, type the following in a behavior script attached to frame 1 (double click the script channel): -

  9. behavior script image

Once files have been linked to cast members in this way, they can be used in the same way as a conventionally imported cast member.

Note that Director does not allow you to distinguish easily between a folder and a file or to get a file’s type other than by using its file extension (which is unreliable on a Mac). For this, you will need to use an Xtra such as the popular BuddyAPI by Magic Modules (http://www.mods.com.au/frameset.htm).

Deleting members from a cast
Before importing, it is usual to clear out any old versions from a cast – it is often easiest to do this as part of a movie’s exit routine. The following example clears all the sound cast members from the cast library assets (it should be placed in the same movie script as the makeSoundList() and loadSounds() functions).
function clearSounds() { // deletes all sounds from cast ‘assets’ cNum = castLib("assets").number; for(var m = 1 ;m <= castLib(cNum).member.count; m++){ if (castLib(cNum).member[m].type == "#sound") { trace("deleting sound: "+castLib(cNum).member[m].name); castLib(cNum).member[m].erase(); } } }

OOP approach to dynamic asset management
This example shows a more generalised approach where assets of different types are loaded into a given cast (the script needs to be placed in a movie script).
//a class definition for an asset manager // needs to be sent a cast library Number to use function AssetManager(libNum){ // *** properties *** this.libNum = libNum; // the cast number to use // *** methods *** this.addAsset = function(fileName){ // makes a new cast member and links the file to it // find out what sort it is var assetType = this.getType(fileName); // make a new member of the right type var memNumber = _movie.newMember(assetType,castLib(this.libNum)); // link the file to the new member member(memNumber).filename = fileName; // send back a reference to the new cast member return member(memNumber); } // a function that finds out what sort of asset // a file contains from the file extension this.getType = function(fileName){ var fileType = ""; // is it a bitmap? if (fileName.indexOf(".jpg")!= -1) { fileType = "bitmap"; } // is it a sound? if ((fileName.indexOf(".aif")!=-1)|| (fileName.indexOf(".wav")!=-1)){ fileType = "sound"; } // send back the type return fileType; } this.clearAll = function() { // deletes all assets from cast for (var m = 1 ; m <= castLib(this.libNum).member.count; m++){ castLib(this.libNum).member[m].erase(); } } }

To use this class, you would first make an object from it: -
var aman = new AssetManager(2);

You would then call the addAsset() method with each file you wanted to add (either in a loop from a configuration file or using all the files in a folder as we did earlier): -
aman.addAsset("low_loop_1.aif");

At the end of your movie (or when you wanted some fresh assets), you could empty a cast by calling the clearAll() function: -
aman.clearAll();

This page was last updated on 17th January 2009