Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 312495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:01:17+00:00 2026-05-12T08:01:17+00:00

I’ve been trying to create an universal asset loader class (with help of the

  • 0

I’ve been trying to create an universal asset loader class (with help of the folks here at stackoverflow), which remembers previousely downloaded assets by storing them in an associative array.

This is the end result:

AssetLoader.as

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

    public final class AssetLoader extends Loader
    {
        public static var storedAssets:Object = {};
        private var postUrl:String;
        private var urlRequest:URLRequest;
        private var cached:Boolean = false;

        public final function AssetLoader(postUrl:String):void
        {
            this.postUrl = postUrl;
            if (storedAssets[postUrl])
            {
                cached = true;
            }
            else
            {
                urlRequest = new URLRequest(Settings.ASSETS_PRE_URL + postUrl);
                contentLoaderInfo.addEventListener(Event.COMPLETE, OnAssetLoadComplete);
            }
        }

        //starts loading the asset
        public final function loadAsset():void
        {
            if (cached)
            {
                loadBytes(storedAssets[postUrl]);
            }
            else
            {
                load(urlRequest);
            }
        }

        //runs when the asset download has been completed
        private final function OnAssetLoadComplete(event:Event):void
        {
            storedAssets[postUrl] = contentLoaderInfo.bytes;
        }
    }
}

Settings.ASSETS_PRE_URL equals “http://site.com/assets/“

Now, my problem is that it is causing the client to crash whenever it tries to retrieve the caches version (the newly downloaded one does work) from the class:

var assetLdr:AssetLoader = new AssetLoader("ships/" + graphicId + ".gif");
assetLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, onShipAssetComplete);
assetLdr.loadAsset();

private function onShipAssetComplete(event:Event):void
{
    var shipImage:Bitmap = Bitmap(event.target.loader.content);
    // Do stuff with shipImage
}

When the cached version is being loaded, I get the following error in dutch: “TypeError: Error #1034: Afgedwongen typeomzetting is mislukt: kan flash.display::MovieClip@5c13421 niet omzetten in flash.display.Bitmap. at GameShip/onShipAssetComplete()” – means something like “type convertion has failed, can not convert flash.display::MovieClip@… to flash.display.Bitmap”.

So, I wonder, how should I extend this loader class and make it return a cached asset the right way? Is my way of storing the asset in the array invalid maybe? Or should I use something else than loadBytes in the AssetLoader method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:01:17+00:00Added an answer on May 12, 2026 at 8:01 am

    I’m not sure why you are insistent on using the contentLoaderInfo if you are busy encapsulating the functionality — go ahead and encapsulate the data too. Also, why store the bytes for an object instead of a simple reference to the actual object?

    Here is an example of what I mean. Take a look at the one degenerate case … that is a request that could be cached but isn’t because the laoder is in the process of loading …

    package 
    {
    
    import flash.display.BitmapData;
    import flash.display.Sprite;
    
    public class TestAssetLoader extends Sprite
    {
        public var loader:AssetLoader;
        public var degenerateLoader:AssetLoader;
        public var cachedLoader:AssetLoader;
    
        public function TestAssetLoader()
        {
            loader = new AssetLoader("picasso_blue_guitar.jpg");
            loader.addEventListener(AssetLoaderEvent.ASSET_LOAD_COMPLETE, handleAssetLoaded);
            loader.loadAsset();
    
            // NOTE: you'll have to think about this case ....
            // where an asset is in the process of loading when you get another request
            // e.g. it isn't yet cached but is already being loaded ... 
            degenerateLoader = new AssetLoader("picasso_blue_guitar.jpg");
            degenerateLoader.loadAsset();
        }
    
        private function handleAssetLoaded(event:AssetLoaderEvent):void
        {
            // here is your content
            // var myImage:Bitmap = Bitmap(event.content);
    
            // This is guaranteed to hit the cache
            cachedLoader = new AssetLoader("picasso_blue_guitar.jpg");
            cachedLoader.loadAsset();
        }
    }
    }
    

    The changed up asset Loader:

    package
    {
        import flash.display.Loader;
        import flash.events.Event;
        import flash.net.URLRequest;
    
        public final class AssetLoader extends Loader
        {
            public static var ASSETS_PRE_URL:String = "";
    
            public static var storedAssets:Object = {};
            private var postUrl:String;
    
            public final function AssetLoader(_postUrl:String):void
            {
                    postUrl = _postUrl;
            }
    
            //starts loading the asset
            public final function loadAsset():void
            {
                if(storedAssets[postUrl])
                {
                    trace("cached load");
    
                    var resource:DisplayObject = storedAssets[postUrl];
    
                    if(resource is Bitmap)
                    {
                        resource = new Bitmap(Bitmap(resource).bitmapData);
                    }
    
                    dispatchEvent(new AssetLoaderEvent(AssetLoaderEvent.ASSET_LOAD_COMPLETE, resource));
                }
                else
                {
                    var urlRequest:URLRequest = new URLRequest(ASSETS_PRE_URL + postUrl);
                 contentLoaderInfo.addEventListener(Event.COMPLETE, OnAssetLoadComplete);
                     load(urlRequest);
                }
            }
    
            //runs when the asset download has been completed
            private final function OnAssetLoadComplete(event:Event):void
            {
                trace("non-cached load");
                var loader:Loader = Loader(event.target.loader); 
                storedAssets[postUrl] = loader.content;
                dispatchEvent(new AssetLoaderEvent(AssetLoaderEvent.ASSET_LOAD_COMPLETE, loader.content));
            }
        }
    }
    

    And the event:

    package
    {
    import flash.display.DisplayObject;
    import flash.events.Event;
    
    public class AssetLoaderEvent extends Event
    {
        public static const ASSET_LOAD_COMPLETE:String = "AssetLoaderEvent_LoadComplete";
    
        public var  content:DisplayObject;
    
        public function AssetLoaderEvent(type:String, _content:DisplayObject, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            content = _content; 
            super(type, bubbles, cancelable);
        }
    
        override public function clone():Event
        {
            return new AssetLoaderEvent(type, content, bubbles, cancelable); 
        }
    
        override public function toString():String
        {
            return "[AssettLoaderEvent] " + type;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 250k
  • Answers 250k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Between the two, porting to Silverlight is going to be… May 13, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer PEP 336 - Make None Callable proposed a similar feature:… May 13, 2026 at 9:20 am
  • Editorial Team
    Editorial Team added an answer Yes, it will handle this if necessary. You really ought… May 13, 2026 at 9:20 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.