O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a New Bitmap Image

To create and display a brand new bitmap image, follow these steps:

  1. Create a BitmapData object.

  2. Set the BitmapData object's pixel colors as desired.

  3. Associate the BitmapData object with a Bitmap object.

  4. Add the Bitmap object to the display list.

Let's try it out!

Our goal is to display a 10 × 10 blue square centered on a 20 × 20 green background. First, we'll create the BitmapData object using the following general code:

new BitmapData(width, height, transparent, fillColor)

The width and height parameters indicate the pixel dimensions of the image, which have a maximum value of 2880. The image dimensions cannot be changed after the BitmapData object is created. The transparent parameter indicates whether the image should support per-pixel transparency (i.e., whether the Alpha channel of each pixel's color value can be set to anything less than 255). If the image does not need to be transparent, then transparent should be set to false because the Flash runtime renders opaque bitmaps faster than transparent ones. Finally, the fillColor parameter indicates the color value that is initially assigned to all pixels in the image.

The image we want to create is 20 × 20 pixels square, does not require transparency, and has a green background. Hence, to create our BitmapData object, we use the following code:

// 0xFF00FF00 means Alpha: 255, Red: 0, Green: 255, Blue: 0
var imgData:BitmapData = new BitmapData(20, 20, false, 0xFF00FF00);

Next, we need to set the color of a 10 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required