<< Chapter < Page | Chapter >> Page > |
The two main bitmap classes
The two main ActionScript classes that are used with bitmaps are:
What is the difference between the two classes?
Briefly, an object of the BitmapData class encapsulates the actual pixel color and transparency values that represent an image. An object ofthe Bitmap class is a subclass of the DisplayObject class, meaning that it can be placed on the display list. A Bitmap object encapsulates a BitmapData object.
A new object of the BitmapData class
The constructor for the Bitmap03 class begins in Listing 1. The constructor instantiates a new object of the BitmapData class with a red opaque background. The constructor parameters for a BitmapData object are:
Draw a yellow cross on the background
A BitmapData object encapsulates a rectangular array of pixel data. The class provides several methods that make it possible for you to get and set individual pixel values in terms of color and transparency.
One of those methods is the method named setPixel that lets you replace (or merge with transparency) an existing pixel with a new pixel on the basis of the horizontal and vertical coordinates of the pixel.
Listing 2 uses a for loop to replace some of the red pixels in the BitmapData object with yellow pixels to form a yellow cross as shown in Figure 1.
var yellow:uint = 0xFFFF00;
for(var cnt:uint = 0; cnt<100; cnt++){
bitmapData.setPixel(cnt, cnt, yellow);bitmapData.setPixel(100 - cnt, cnt, yellow);
} //end for loop
The setPixel method
The setPixel method requires three parameters. The first two are the horizontal and vertical coordinates of the pixel whose colorvalues are to be set. The third is an unsigned integer value that specifies the color that will be assigned to the pixel at that location.
Without getting into the details at this point, suffice it to say that the value assigned to the yellow variable in Listing 2 specifies the visible color of yellow.
Encapsulate the BitmapData object in a Bitmap object
The constructor for the Bitmap class has three parameters, but they all have default values. The first parameter, if provided, must be areference to an object of type BitmapData .
Continuing with the constructor for the Bitmap03 class, Listing 3 instantiates a new Bitmap object passing the BitmapData object prepared in Listing 2 as a parameter.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?