Embed bitmap in ActionScript3

风流意气都作罢 提交于 2019-12-02 06:05:03

问题


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]



来源:https://stackoverflow.com/questions/10768780/embed-bitmap-in-actionscript3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!