Embeding and reading image assets when deploying t

2019-06-06 07:00发布

问题:

In other platforms i could just use something like [Embed(source="logo.gif")] or @:bitmap, but it seems there is no option for that for Windows/Other Cpp platforms.

I tried to use the EmbedAssets lib but it's outdated.

I also tried using the nmml file resource tag. With this i could get the image as haxe.sys.io.Bytes, but to use i need to convert haxe.sys.io.Bytes to nme.utils.ByteArray. I have not found a way to do this.

So, what can i do to embed images on a haxe/nme project when deploying to Windows?

回答1:

In addition to openfl.Assets, OpenFL supports @:bitmap, @:sound, @:font and @:file embed tags.

The former requires <assets path="to/assets" /> in your project XML file, and on Windows, will copy the files alongside your executable.

The embed tags require that your asset files are in your source path based on the way they are embedded, so use <source path="to/assets" /> in the project file.

Here is an example that uses the @:bitmap tag:

package;


import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;

@:bitmap("nme.png") class Image extends BitmapData {}


class Main extends Sprite {


    public function new () {

        super ();

        var bitmap = new Bitmap (new Image (0, 0));
        addChild (bitmap);

        bitmap.x = (stage.stageWidth - bitmap.width) / 2;
        bitmap.y = (stage.stageHeight - bitmap.height) / 2;

    }


}

Using embed tags, the asset will be inside your executable.



回答2:

With NME or OpenFL you should just be able to call Assets.getBitmapData("assets/myImg.png"); without needing to add the assets to the NMML file (though you may need to add <assets path="Assets" rename="assets"/> or rename the assets directory). You should then be able to add this bitmapData to a bitmap object on the display list.

For example:

var bd:BitmapData = Assets.getBitmapData("assets/myImg.png");
var bitmap:Bitmap = new Bitmap(bd);
addChild(bitmap);