2

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?

3 Answers 3

17

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;
5
  • If i do new Picture() i get TypeError: Error #1007: Instantiation attempted on a non-constructor
    – Hophat Abc
    May 27, 2012 at 0:55
  • Are you still doing EMBED not Embed as per example?
    – Neil
    May 27, 2012 at 9:30
  • This seems to be the right answer for everyone else so I'll accept
    – Hophat Abc
    May 27, 2012 at 22:41
  • 1
    I got it working with BM = new BMClass() as Bitmap;. I have no idea why it didn't work before but now it does. Thanks for your help.
    – Hophat Abc
    May 28, 2012 at 5:46
  • Excellent, glad you're sorted :)
    – Neil
    May 28, 2012 at 8:41
3

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();
1
  • As I said above, I can't do new AssetClass();
    – Hophat Abc
    May 27, 2012 at 0:56
0

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.