Recipe 8.2. Adding a
Bitmap to the Display List
Problem
You have created a BitmapData and now
want to make it visible.
Solution
Create a Bitmap using the
BitmapData and add that to the display list.
Discussion
To make anything visible in an application in
ActionScript 3.0, you must add it to the display list. (See
Chapter 6
for a full discussion of the display list.) You add objects to the
display list by calling the addChild( ) method from the main
application class, or any other object that has already been added
to the display list. However, the addChild( ) method only
accepts objects that are subclasses of
flash.display.DisplayObject. The BitmapData class is
descended only from Object, so you may not add it to the
display list directly.
To add it to the display list, use the
flash.display.Bitmap class, which is a subclass of
DisplayObject. It is a sort of wrapper for
BitmapData, allowing a BitmapData to be
displayed.
When you create a new instance of Bitmap
by calling its constructor, you pass in a reference to a
BitmapData. Then you can add the Bitmap to the
display list using addChild( ). The following example
creates a BitmapData with a red fill, and displays it via a
Bitmap:
var bitmap:BitmapData = new BitmapData(100, 100, true, 0xffff0000);
var image:Bitmap = new Bitmap(bitmap);
addChild(image);
See Also
Recipe
8.1 for how to create a bitmap.
|