Dynamic assets in Flash – an overview
Flash has a much more restricted range of asset types it can use than Director and is also less flexible about preloading some types of files (sounds and digital videos). Flash is also unable to investigate file systems at all – so that loading all files in a folder either requires Director or the Adobe Integrated Runtime (AIR) environment (for local files) or php (for files on a server).
Example 1 – an automatic slideshow
The following example uses a custom class to load and hold external images from a given list (this will be supplied by a configuration file in future examples). Note that Flash 9.0 includes the Loader class which will load any display assets, this is not available in earlier versions which have to use either the MovieClip or MovieClipLoader classes. The Loader and MovieClipLoader classes are able to give more information about download progress and (importantly) errors than our simple example.
In this example, we will load our images and then use our simple timer object which can cause images to switch at a regular interval.
Further developments
- Make a new movie and create 2 extra layers, naming them scripts, labels, and display.
- Select frame 10 in all of your layers and then choose Blank Keyframe from the Insert menu.
- Do the same for frame 20.
- Select Frame 1 of the labels layer and then, using the Frame palette, give it the label init.
- Select Frame 10 of the labels layer and give that the label main.
- Lock the scripts and labels layers. Your timeline should look something like this: -
- Save your movie as slideshow_auto.fla in a new project folder.
- Choose New from the file menu and then select ActionScript File. This opens a new .as external script file – the only place you are allowed to create classes.
- Carefully type the following: -
- Save the file as AssetManager.as in the same folder that the flash movie is in. Make sure the file name is exactly the same as the Class name.
- Copy the Timer.as file from the previous tutorial and put it in the same folder.
- In the folder make a new folder called images and put 3 jpg files in it.
- Select Frame 1 of the scripts layer. In the Actions panel type the following (you will need to change the image file names for those you are using); -
- Select Frame 3 of the scripts layer and insert a blank Keyframe.
- In the actions panel, type the following:-
- Select the frame actions for Frame 10 of the scripts layer. Type the following: -
- Check your code syntax and close the Actions pane. Your movie is finished.
- Test it by pressing the CONTROL and ENTER keys together.

class AssetManager{
// assets' references are held in an array
// *** properties ***
var assets:Array;
var owner:MovieClip;
// *** methods ***
// the constructor function
public function AssetManager(mClip:MovieClip){
// set up the array
this.assets = new Array();
this.owner = mClip;
}
// loads an asset from a given location
public function addAsset(fileName:String):MovieClip{
// make an empty movieclip in a free level
var mcName:String = "asset"+this.getAssetCount()+"_mc";
var myClip:MovieClip = owner.createEmptyMovieClip(mcName,this.getAssetCount()+1);
myClip.loadMovie(fileName);
// hide the clip
myClip._alpha = 0;
// store the reference in the array
this.assets.push(myClip);
// and send it back in case it's wanted
return myClip;
}
// tests if all assets have loaded yet
public function loaded():Boolean{
var loadDone:Boolean = true;
// go through each in turn using a loop
for(var i:Number = 0; i‹getAssetCount();i++){
if(assets[i].getBytesLoaded()!=assets[i].getBytesTotal()){
loadDone = false;
}
}
return loadDone;
}
// gets an asset based on its number
public function getAsset(id:Number):MovieClip{
// send back the asset in that position in the array
return this.assets[id];
}
// gets an asset based on the number
public function getAssetCount():Number{
// send back the number of assets
return this.assets.length;
}
}
// script for init
// make a new asset manager
var pics:AssetManager = new AssetManager(this);
// load 3 images
// change the file names to those of your images
pics.addAsset("images/impact.jpg");
pics.addAsset("images/iss.jpg");
pics.addAsset("images/titan.jpg");
// see if all the assets have loaded yet
if (pics.loaded()) {
// all loaded, go on
this.gotoAndPlay("main");
} else {
// not yet, wait for loading to finish
this.gotoAndPlay(2);
}
// make a new Timer with an interval of 3 seconds
var timer:Timer = new Timer(3000, this);
// set the first image number
var curImage:Number = 0;
// set up the first image
setUpClip(pics.getAsset(curImage));
// start the timer
timer.startTimer();
// hold the movie here
stop();
// the update function which is called by the timer
function update():Void {
//hide the last picture
pics.getAsset(curImage)._alpha = 0;
// get the next image's number
curImage++;
if (curImage>=pics.getAssetCount()) {
curImage = 0;
}
// set up this image
setUpClip(pics.getAsset(curImage));
}
// resizes a clip to the same dimensions as the stage
function setUpClip(myClip:MovieClip):Void {
myClip._height = Stage.height;
myClip._width = Stage.width;
myClip._alpha = 100;
}
Look at how I check if the images are completely loaded in the loaded() function in the AssetManager. It would be nice to have another similar function in our asset manager which returned the percentage loaded when it was called. See if you can add this and then use it to give feedback to the user on how the asset download is going either by text or by a progress bar.
This page was last updated on 31st May 2008