Working with bitmap assets

Using bitmap assets, instead of loading bitmaps from a web URL, a file system path, or generating the bitmap data manually, is another possibility for rendering bitmap images. In some cases, bitmap assets will be embedded in the generated binary, and in others, they will be included as separate files. However, the API for working with either option is the same, allowing you to write code once and target many platforms.

Specifying bitmap assets

In a Lime project.xml file, include bitmap image files using the <assets> element. Bitmap assets may be of the bitmap type only.

  • image
<assets path="assets">
    <image path="img/MyImage.png" id="MyImage" />
    <image path="img/AnotherImage.jpg" />
</assets>

Using bitmap assets

The following code works with a bitmap asset with the id "MyImage". The code gets an instance of the bitmap asset and passes it to a new Bitmap display object:

import openfl.utils.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Sprite;

class BitmapAssetExample extends Sprite
{
    public function new()
    {
        super();
        var bmd:BitmapData = Assets.getBitmapData("MyImage");
        addChild(new Bitmap(bmd));
    }
}

More Help topics

results matching ""

    No results matching ""