Assets (HTML5): how to laod them manually avoiding preloading?

I have several PNG files in the project that I want to load manually and avoid preloading, since it slows down the startup. Let’s say, I want to preload the files for the main menu, etc, but then load one image per level before the level starts.

As far as I understand, I can create a separate assets library and add the files that I don’t want to be preloaded there and then preload manually, like it’s described here: XML Format

<library name="myOtherLibrary" preload="false" />

But when I preload it, all the files added to that library will be preloaded. So if I want EACH of the files to be preloaded separately, I’ll have to add a library for each file and then add one file to each library, is it correct?

Is there a better way to achieve my goal: load (PNG) files when I need it without preloading?

Thanks!

Can you use the Loader class on your project?

Somethng like this:

var loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onOk);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError);

loader.load('path to your PNG');

function onOk(e:Event):Void {
  // load into a bitmap or you can add the loader itself to the display
  var bmp:Bitmap = cast this._loader.content;
}

function onError(e:Event):Void {
  // handle loading failure
}
1 Like

Yes, thanks, will try this one!

<assets path="assets/islandsbig" include="*" embed="false" />

When preload is set to false (or embed to false) the assets should be available for loading like so:

Assets.loadBitmapData("assets/islandsbig/"+id+".png").onComplete(
			function(bmd:BitmapData){
				
				var b = new Bitmap(bmd);
				b.smoothing = true;
				b.scaleX = 0.33;
				b.scaleY = 0.33;
				addChild(b);

			}
		);

Alternatively, I have used an asset manifest at runtime:

var manifest:AssetManifest = new AssetManifest(); 
manifest.addBitmapData("assets/products/" + product.id + ".png", product.id);

AssetLibrary.loadFromManifest(manifest).onComplete (function (library) {
			Assets.registerLibrary("mynewlibrary", library);
});
var bData = Assets.getBitmapData("mynewlibrary:"+ product.id);
2 Likes

Thanks! Sounds exactly like what I wanted to have :slight_smile: