Tutorial details: BitmapData - Flash 8 (Maelstrom) Tutorial
Difficulty Level: Intermediate
Requirements: Flash MX 2004 and Flash 8 player
Assumed Knowledge: Having read this tutorial
File(s) to Download: stars.zip and bmap.zip
Online Example: Stars
The BitmapData Class
As this is a pre-release tutorial, amendments are likely to be made and so I would suggest you check back for occasional updates.
To get up and running, publishing Flash 8 movies via the Flash 7 IDE, please read through my first Flash8 [Maelstrom] tutorial
What is the BitmapData class all about?
flash.display.BitmapData allows us pixel-level control of bitmaps. What does this mean? Well, in brief we could do the following:
- Copy and paste an entire image, part of an image, or even individual pixels
- Identify and/or change the colour of an individual pixel or group of pixels
- Apply one of the new Flash filters
- Create random pixels (noise & perlin noise)
…and more!
It should also be noted that we can use the BitmapData class with video.
Creating an Instance of the BitmapData class
We create an instance of the class like so:
bmap = new flash.display.BitmapData(100,100, false, 0);
The BitmapData class takes up to 4 parameters:
- Width (number, in pixels)
- Height (number, in pixels)
- Alpha values (boolean)
- Default background colour fill (number)
So, as you can see I'm creating a square 100*100, not allowing alpha, and specifying 0 (black) as the background colour.
From now on, for brevity, I will refer to the instance of any BitmapData class we create as BMD.
Note: At this point in time I'm not sure whether the alpha value only relates to the BMD's background alpha, or whether it also has something to do with allowing transparent pixels in an image.
Copy & Paste
Perhaps the best example to start with is this basic copy & paste operation. Think of the possibilities allowed to you by being able to copy and paste pixels from images,
on to your BMD. For example, in Flash MX 2004 and lower, it is not possible to load an external picture in to a MovieClip, and then duplicate that MovieClip to see a duplicate
of that picture. Any externally loaded pictures have to be re-loaded if you want to see more than one copy. Well, not any more!
We can of course also manipulate images that have been imported manually in to Flash at author-time. As this is perhaps the easiest way to get started we'll use an imported
image in the first example.
Grab the first sample fla (bmap.zip) and take a quick look at it before you proceed.
There are three objects involved in this "copy and paste" process:
- The source bitmap
- Creating the BitmapData instance
- The destination MovieClip
For our destination MovieClip we have a further two options:
- Specifying a clip that's been created manually at runtime (and may have a width/height of more than zero)
- Creating a destination MovieClip using createEmptyMovieClip
In our first example fla I have created the destination clip manually at author-time.
|