Alex Jeffery

HomeArchivesAboutFeed

Embed A Custom Bitmap Using Haxe And FlashDevelop

Recently I started learning Haxe to program Flash games using FlashDevelop. I wanted to embed a custom bitmap image to use as my player sprite. It took me two days to figure this out, so I am writing a quick tutorial in the hope it will save someone else some time.

Add a png file to your FlashDevelop project
Right click your png file and click the “Add to library” option
Right click your png again and select the new “Options” option that is now active
Check the “Embed as Bitmap instead of clip” check box
Uncheck the “Auto-generate ID for attachMovie():”

Enter a sensible name Eg “PlayerBitmap”.
Prefix this name with the package your Main is found in. Eg “org.alexjeffery.PlayerBitmap”

The need to prefix your embeded sprite name with the package name tripped me up when I was trying to get this to work. If you don’t do it the code will compile fine but you will get an invalid bitmap exception when you try to create your custom BitmapData class.

Add the following code to your project

  1. class PlayerBitmap extends BitmapData {public function new(){super(0,0);}}
  2. var bitmapData:BitmapData;
  3. bitmapData = new PlayerBitmap();
  4. var bitmap:Bitmap = new Bitmap(bitmapData, flash.display.PixelSnapping.AUTO, false);
  5. bitmap.x = 100;
  6. bitmap.y = 100;
  7.  
  8. flash.Lib.current.addChild(bitmap);

Build and test your movie.
You should see your custom sprite on the screen.

References:
Embed bitmap in haxe
How to embed and display a png with FlashDevelop, Haxe and Swfmill

Comments are closed.

Alex Jeffery