Dynamic assets creation by full class name from swf library is broken

I am using some external swf libraries at openfl project for html5 target. I have embedded them with preload=“true” and generate=“true” options. All assets were placed at bin directory in unpacked swflite .bin format.

Before last openfl big update I have easily instantiated assets by code like this:

var clip:MovieClip =
    cast Type.createInstance(Type.resolveClass("full.class.Name"), []);

Now I see that libraries turns to (unpacked) zip archives that are downloaded by browser, but assets classes became unavailable by name. My small investigation shows that adding direct call to constructor like this:

var preloadDummy = new full.class.Name();

brings followed creation of assets by classified class name to life. But this ruins my approach of dynamic assets creation.

It looks like some sort of ‘unused assets optimization’. If I don’t use them directly at my code they are also unavailable for creation by full path. Is it a new openfl option that should be configured?

I have removed generate=“true” option because it have no sense for my case. All children are discovered by getChildByName() or getChildAt(). But problem still persists.

Moreover, straight import of asset as class with full name allows to get reference by resolveClass() method, calling of constructor is not needed…

I have solved my problem. AS3 approach with

var asClass:Class = getDefinitionByName("full.class.Path");
var asClip:MovieClip = new asClass() as MovieClip;

that is implemented in Haxe as

var hxClass = Type.resolveClass("full.class.Name");
var hxClip:MovieClip = cast Type.createInstance(hxClass, []);

will work seamlessly on flash target only.

When using OpenFL at html5, all resources loaded by external libraries doesn’t appears as classes by default. You can access only to those of them that have been embedded directly to the application bundle in compile time.

To access dynamic libraries you should use:

var hxClip = Assets.getMovieClip("lib-name:full.class.Name");

or via loaded library assets container:

Assets.loadLibrary("lib-name").onComplete(function(lib:AssetsLibrary) {
    var hxClip = lib.getMovieClip("full.class.Name");
});

This works both at flash and html5 targets and probably at all other.

1 Like

Does it work if you use new full.class.Name() instead?

Assets.loadLibrary("lib-name").onComplete(function(lib:AssetsLibrary) {
    var hxClip = new full.class.Name();
});