Not sure understood Assets.loadX

Hey there !

I’m make some test with openFl and i’m not sure to understand the philosophy behind the Assets.loadImage, font, etc.

I make a new openFl project with this configuration :

<?xml version="1.0" encoding="utf-8"?>
	<!-- NMML reference: -->
	<!-- metadata, make sure 'package' is at least 3 segments (ie. com.mycompany.myproject) -->
	<meta title="LoadingTest" package="LoadingTest" version="1.0.0" company="" />
	<!-- output -->
	<app main="Main" file="LoadingTest" path="bin" />
	<window background="#000000" fps="60" />
	<window width="800" height="480" unless="mobile" />
	<window orientation="landscape" vsync="false" antialiasing="0" if="cpp" />
	<!-- classpath, haxe libs -->
	<source path="src" />
	<haxelib name="openfl" />
	<haxelib name="actuate" />
	<!-- assets -->
	<icon path="assets/openfl.svg" />
	<assets path="assets/img" rename="img"/>
	<!-- optimize output
	<haxeflag name="-dce full" /> -->

So I understand that all assets in assets/img are embbed and can use it with Assets.getImage(“img/elephant.jpg”) (or other) with the rename path Assets/img to only img/

But if i want to load other files asynchronously, and not necessary in “assets/img”. how make this properly ? (like no-embbed assets or an external assets). Need to use UrlLoader.hx or other class like this ?

I try to use Assets.loadImage(“Assets/other/elephant.jpg”), but seems to load only files into “assets/img”. Other path throw an error (like “assets/other/elephant.jpg” for exemple.) saying “file not found” to resume.

And this code find the file but throw an error :


import openfl.display.Loader;
import openfl.display.Sprite;
import openfl.Lib;
import openfl.Assets;
import openfl.display.BitmapData;
import openfl.display.Bitmap;

 * ...
class Main extends Sprite 

	public function new() 
		var test  = Assets.loadBitmapData("img/elephant.jpg");
	private function onprogress(a : Int, b : Int) : Void
		trace (a + "/" + b);
	private function onError(test : Dynamic) : Void
	private function onLoaded(bmd : BitmapData) : Void
		var bm : Bitmap = new Bitmap(bmd);


The error throw By Lime.AssetsLibrary.hx (sorry, french version :p)

[Fault] exception, information=TypeError: Error #1034: Echec de la contrainte de type : conversion de __ASSET__img_elephant_jpg@3f10341 en impossible.

Why this error ?

How to manage external/not embbed assets properly ?
There are a limit to embbed assets ? (like a max Cache size, or other limitation ?)

I’m using :

  • haxe 3.4.3
  • lime 5.7.1
  • openfl 6.2.2
  • actuate 1.8.7

Targetting Flash (for test only i want to targetting android and html5 soon)

Thanks and sorry for my bad english !

We embed assets into your SWF on Flash (by default), or preload on HTML5.

For example, lets say you have a file located at “Assets/image.png”

<assets path="Assets" rename="assets" />

The above tag in your project file will tell the tools to embed on Flash, and preload on HTML5, so that you can access your image immediately at runtime:

var bitmapData = Assets.getBitmapData ("assets/image.png");

You can also load it asynchronously:

Assets.loadBitmapData ("assets/image.png").onComplete (function (bitmapData) {

There is also an “embed” attribute you can use in your project file, to override the default embed behavior (which varies per target) and force it to always embed, or not. Generally, you might want this for preventing the SWF embed / HTML5 preload behavior

<assets path="Assets" rename="assets" embed="false" />

This means the asset files are copied, but not preloaded, so they are available only asynchronously using Assets.load*

openfl.utils.Assets only accesses files that were known at build time, or to additional asset libraries, if you add them at runtime. If you want to load an arbitrary file that is not a part of your project, you can use things like BitmapData.loadFromFile or openfl.display.Loader

var loader = new Loader ();
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, function (_) {
    var bitmapData = cast (loader.content, Bitmap).bitmapData;
loader.load (new URLRequest ("other/image2.png"));
BitmapData.loadFromFile ("other/image2.png").onComplete (function (bitmapData) {

Thank you for this explanation. :+1:

It confirms what I understood !