Loading and handling external files in Flash: ExternalAssets.as

Here’s another helper class that loads one or multiple file(s) separately or all in a batch. There is one special feature: this class can check the filesize of each file (without fully loading them) and therefore it can show the loading progress of the whole batch not just single file. The loaded data is stored so the file or its size is never loaded twice.

This class uses my ContentLoader as the actual file loader. Read more about the ContentLoader »

Example:


_externalAssets = new ExternalAssets();

//add listeners

_externalAssets.addEventListener(Event.COMPLETE, assets_COMPLETE);

_externalAssets.addEventListener(Event.CHANGE, assets_CHANGE);

_externalAssets.addEventListener(ErrorEvent.ERROR, assets_ERROR);

_externalAssets.addEventListener(ExternalAssets.FILESIZE_COMPLETE, assets_FILESIZE_COMPLETE);

//add files to load

_externalAssets.add("fileID", "image1.jpg", Asset.TYPE_MEDIA);

_externalAssets.add("fileID2", "image2.jpg", Asset.TYPE_MEDIA);

_externalAssets.add("fileID3", "image3.jpg", Asset.TYPE_MEDIA);

_externalAssets.add("fileID4", "text.txt", Asset.TYPE_TEXT);

//load all files

_externalAssets.loadAssets();

//or load one file

_externalAssets.loadAsset("fileID4");

//or first get the total filesize

_externalAssets.getFileSizes();

//or the size of one file

_externalAssets.getFileSize("fileID2");

//after the file have been loaded, get a file. For example, if it was an image

addChild(_externalAssets.getData("fileID"))

Download source and example files.

Kommentoi