Load folder of assets later


#1

Hello!

I want to load some assets dynamically. Using :

<assets path="assets/img" library="images" />
<library id="images" preload="false" />

trace(Assets.getLibrary("images")); // passed
trace(Assets.list(AssetType.IMAGE)); // passed ( names such as assets/img/1.jpg and so on )

var bmp = new Bitmap(Assets.getBitmapData("assets/img/1.jpg"));  // crash ID
var bmp = new Bitmap(Assets.getBitmapData("images:1.jpg"));  // crash ID
var bmp = new Bitmap(Assets.getBitmapData("images/1.jpg"));  // crash ID

whats im doing wrong?


#2

Hi in4core.

Try this:

var bmp = new Bitmap(Assets.getBitmapData("images:assets/img/1.jpg"));

I’m not sure though if it’s the correct way of doing things because it seems a little awkward. :wink:


#3

Also, on HTML5 you’ll need to allow time for the library to load, instead of getting immediately:

Assets.loadLibrary ("images").onComplete (function (_) {
    ...
}).onError (function e) {
    trace (e);
});

You can also access assets directly from the library, if you prefer. That removes automatic support for the openfl.utils.Assets cache, though:

var library = Assets.getLibrary ("images");
var bmp = library.getBitmapData ("assets/img/1.jpg");