开发者

How to manage loaded assets?

开发者_如何学编程I'm working on an AS3 project that must load a lot of external files; images, sound clips, movie clips etc. The program first loads an XML file that contains the file names of all assets needed by each class. From that point on I'm not sure what would be the best way to distribute those assets.

I thought about making a dedicated assets class that will load everything the XML object describes, and then pass it to each class' constructor so they can access the objects they need. This seems like the "tidiest" option, but is there a best practice way for managing loaded assets?


You could create a class that handles loading the assets and storing them as bytes. Then you can get an asset from that class via a reference(e.g. name) and do what you need with it. I've created an example to demonstrate this:

package 
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.utils.ByteArray;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        private var _assets:Assets;
        private var _counter:int;
        private var _assetsXml:XML;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            _assetsXml = <assets>
                             <asset name="chrysanthemum" url="assets/chrysanthemum.jpg"></asset>
                             <asset name="kalimba" url="assets/kalimba.mp3"></asset>
                             <asset name="wildlife" url="assets/wildlife.wmv"></asset>
                         </assets>;

            _assets = new Assets();

            for each(var asset:XML in _assetsXml.children())
            {
                _assets.addEventListener(Event.COMPLETE, onAssetComplete);
                _assets.load(asset.@name, asset.@url);

            }// end for each

        }// end function

        private function onAssetComplete(e:Event):void
        {
            if (++_counter == _assetsXml.children().length()) addImage();

        }// end function

        private function addImage():void 
        {
            var loader:Loader = new Loader()
            loader.loadBytes(_assets.getAssetBytesByName("chrysanthemum"));
            addChild(loader);

        }// end function

    }// end class

}// end package

import flash.events.Event;
import flash.events.EventDispatcher;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.utils.ByteArray;

internal class Assets extends EventDispatcher
{
    private var _assetLoaders:Vector.<AssetLoader>;

    public function Assets():void 
    {
        _assetLoaders = new Vector.<AssetLoader>();

    }// end class

    public function load(name:String, url:String):void
    {
        var assetLoader:AssetLoader = new AssetLoader(name);
        assetLoader.addEventListener(Event.COMPLETE, onAssetLoaderComplete);
        assetLoader.load(url);

    }// end function

    public function getAssetBytesByName(name:String):ByteArray
    {
        for each(var assetLoader:AssetLoader in _assetLoaders)
        {
            if (assetLoader.name == name) break;

        }// end for each

        return assetLoader.bytes;

    }// end function

    private function onAssetLoaderComplete(e:Event):void
    {
        var assetLoader:AssetLoader = e.target as AssetLoader;
        _assetLoaders.push(assetLoader);

        dispatchEvent(new Event(Event.COMPLETE));

    }// end function

}// end class

internal class AssetLoader extends EventDispatcher
{
    private var _name:String;
    private var _url:String;
    private var _bytes:ByteArray;

    public function get name():String { return _name }
    public function get url():String { return _url };
    public function get bytes():ByteArray { return _bytes };

    public function AssetLoader(name:String):void
    {
        _name = name;

    }// end function

    public function load(url:String):void
    {
        _url = url;

        var urlLoader:URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        urlLoader.addEventListener(Event.COMPLETE, onUrlLoaderComplete);
        urlLoader.load(new URLRequest(_url));

    }// end function

    private function onUrlLoaderComplete(e:Event):void
    {
        var urlLoader:URLLoader = e.target as URLLoader;
        _bytes = ByteArray(urlLoader.data);

        dispatchEvent(new Event(Event.COMPLETE));

    }// end function

}// end class
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜