Search code examples
actionscript-3actionscriptbitmapembed

Embed bitmap in ActionScript3


How can I embed a bitmap in Actionscript 3 and get the BitmapData?

public class MyGame extends Sprite {
    [EMBED(source="Assets/helicopter1.png")] private static var BMClass:Class;
    public function MyGame() {
        var BM:Bitmap = new BMClass();
        var BMData:BitmapData = new BitmapData(BM.width, BM.height);
        BMData.draw(BM)
    }
}

I've tried everything. If I ever try to instantiate the embedded class (new BMClass();) I get this error:

TypeError: Error #1007: Instantiation attempted on a non-constructor..

If I use

[EMBED(source="Assets/helicopter1.png")] private static var BMClass:BitmapData;

or something similar the BitmapData is null.

Edit:

So I figured out that the embedded data is null, but I can't figure out why. What did I do wrong in the embedding?


Solution

  • Looks like you are embedding correctly if you don't get an error transcoding. You should be able to get the bitmapData directly from the bitmap:

    [Embed(source="picture.jpg")]
    private var Picture:Class;
    
    // create a bitmap of the embedded
    var pic:Bitmap = new Picture();
    
    // add to display list
    addChild(pic);
    
    // if you need to get the bitmapData for something else
    var bitmapData:BitmapData = pic.bitmapData;