Embed bitmap in ActionScript3

2019-02-25 14:33发布

问题:

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?

回答1:

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;


回答2:

You don't need to instantiate as BitmapData and draw - you can simply:

[Embed(source="Assets/helicopter1.png")]
private var AssetClass:Class;

var bitmap:Bitmap = new AssetClass();


回答3:

In some editors (at least my version of Intellij) the Embed tag is case sensitive. I got the exact same error you have when using [EMBED] but it worked great when I switched to [Embed]