Sunday, August 8, 2010

Working with Bitmaps in Actionscript 3: Part 1

In this tutorial we will lern how to create a BitmapData object and place it on the stage.

Until Flash 8 came out, Flash was mainly a vector program - we could still load bitmap images and place them on stage, however that's pretty much it. Now, we have BitmapData class, which is very useful for working with what we see on the screen.

The first step to create a new BitmapData object is to import flash.display.BitmapData. Creating a new object is simple:

var mybitmap:BitmapData = new BitmapData(width, height, transparent, fillColor);

Width and height parameters define width and height. Transparent is a boolean, which defines whether our BitmapData will or will not have an alpha channel. FillColor is the color our object is filled with. Width and height are necessary parameters, transparent and fillColor are optional, and their default values are true and 0xFFFFFFFF.

In the next example we are creating a rectangle that is completely transparent:

var mybitmap:BitmapData = new BitmapData(200, 100, true, 0x00FFFFFF);

Good, we've created our BitmapData object, but it's not displayed on the stage. To make something visible in AS3, it needs to be added to the Display list. Objects are added using addChild() method. However, addChild only wants to work with objects that are flash.display.DisplayObject subclasses, which BitmapData isn't. No worries, we can create a new Bitmap object, which takes the data from our BitmapData. Then we can add it to the stage.

var mybitmap:BitmapData = new BitmapData(200, 100, true, 0xffff0000);
var image:Bitmap = new Bitmap(mybitmap);

But image isn't always a rectangle filled with one color. It can be a photo, a drawing, anything that can be shown on the screen. We will work on that in the next tutorials! Thanks for reading.


Working with Bitmaps in Actionscript 3: Part 2
Working with Bitmaps in Actionscript 3: Part 3

No comments:

Post a Comment