2012-12-10

Caching loaded images in AS3

Link to example project
Link to stack overflow

So, I read this question on StackOverflow about a guy who wanted to load images several times but didn't want to have them placed inside a swf-file or a swc-file (or rather wanted to have the swc created at runtime).

Storing images inside a cache in that way isn't super easy so I thought I would give it a try and see how a solution to that would work. I started out with a simple Static ImageManager but shortly found that it wasn't nearly as easy as I first thought it would be due to race conditions and as3 being rather limited to how you can work with loaded content, queuing loads etc.

I ended up creating two classes and an extra Main-class to provide as an example:

ImageManager (static class, keeps url as id for the image sand stores loaded images bitmapdata)
FileLoader (dynamic instances, does the actual load of objects and then notifies when it is done)

Since there is no easy way of storing files on this blog, I had to create some online storage service [chose Skybox today].

Anyways, here is the link to my example project, source code is "pasted" below for "completeness" if you just wanna see the files and don't wanna download the entire project.

Main.as - link to pastie
ImageManager.as - link to pastie
FileLoader.as - link to pastie

Inga kommentarer:

Skicka en kommentar