Flash 8 Application of BitmapData class

Source: Internet
Author: User

What is the BitmapData class?

Flash.display.bitmapData allows us to use pixel hierarchies to control bitmaps, which means, well, in the following brief we'll outline what it can do.
• Copy and paste the entire image, part of the pattern. or every single pixel.
• Identify or change the color of pixels or pixel groups.
• Apply a new flash filter.
• Create random pixels (noise or perlin noise), and so on.
It can also apply bitmapdate to the video.

to create an instance of the BitmapData class

We use the following method to create a BitmapData instance
Bmap=new Flash.display.BitmapData (100,100,false,0);
The BitmapData class provides four parameters
Width (number value, like pixels)
Height (number value, like ICAO)
Alpah Value (Boolean boolean value)
Default background color fill (a background color that is added with the defaults) (number value)
So from the code above, we can see that I created a 100*100 square that does not allow alpha transparency, specifying 0 as the background color, which is black.
Note: For the moment, I am not sure if the alpha value is related only to the background alpha of BMD, or whether it only allows the pixels in the image to be transparent.

Copy & Paste
Copy and paste

Perhaps the best example would be to start with the most basic copy and paste operation. Imagine copying and pasting pixels from images through BMD. For example, in flashmx2004 or earlier versions, it is not possible to load images into movieclip and then copy them in MovieClip to see the copied images. Because any loaded images must be reloaded if you want to see more copies of the content.
We can, of course, manually import pictures at the time of authoring, which is perhaps the easiest way, and we'll use this in the first example.

Download the first sample file, sample fla (bmap.zip), and take a quick look at this file before you proceed.
There are three objects in this section about copy and paste.
1. Source image (Origin)
2. BitmapData instance created (BMAP)
3. Target MovieClip (DEST2)
For the creation of the target MovieClip we can choose two ways
1. Create a MovieClip manually, in this case, the width is set to 100.
2. To use Createemptymovieclip () to create.
In our first example, we used the first method to create a movieclip at the time of authoring.

Example 1:
Example 1


We have placed two MovieClip in the home scene.
The first MovieClip contains a picture, the size is 100*100 pixels, and the instance name is source. The second movieclip contains an orange shape, which is also 100*100 pixel, and the instance of this MC is named Dest2.
To copy the entire image, we can do the following:

Code:

Bmp=new Flash.display.BitmapData (100,100,false,0);
Bmap.draw (source);
Dest2.attachbitmap (bmp,1);

In the first line we created the actual BMD, called BMP, and its size is the same size as our image. In the second line, copy the image on BMD, and you can use the following syntax to place a movieclip in BMD.
Somebmdinstance.draw (Somemovieclip);
Finally, we would like to see a copy of the results on BMD, where you can't see the results until you attach it to a movieclip. The following syntax:

Code:

Destinationmc.attachbitmap (somebmdinstance,depth);

Note: Attachbitmap actually has 4 parameters, all of which are as follows:
Attachbitmap (Bmp:bitmapdata, Depth:number, pixelsnapping:string, Smoothing:boolean)
The first two parameters are a reference name for the BitmapData class, and one is the depth.
The third parameter pixelsnapping (pixel alignment) has three choices,
Auto: Pixel alignment When the bitmap is not deformed or rotated.
Always: pixels are aligned regardless of whether the bitmap has been distorted or rotated.
Never: Never snap to pixels.
Auto is the default.
Fourth parameter: Smoothing (smooth): It is a Boolean value to determine whether the image of the thumbnail is smooth.

In this case we only need to use the first two parameters. When you run the test, you will find that the source image has been copied to the target MC.

So now we're going back to the movieclip that we started by manually creating or using Createemptymovieclip () when we used authoring. When we paste the example of BMD into the MC we created, we think of some questions, such as what happens if the BMD instance is larger than the source movie clip, or if the target MC (DEST2) is less than the BMD instance, and so on.

Here we'll use a few pictures to illustrate the problem:
It is important to note that the source and dest2 targets in the text below indicate that the target MC does not refer to the movie clip itself, but to the content within the movie clip, if you want to specify a movie clip to add a description.

Souce Source Contents: 100*100px (the movie clip itself is not scaled);
Dest2 the content of the target clip: 100*100px (the movie clip itself is not scaled)
BMD instance: (this size refers to the size specified at the time of creation, see Code) 100*100px.

Here's what we've been experimenting with:

Souce Source Contents: 100*100px (the movie clip itself is not scaled);
Dest2 the contents of the target clip: 50*50px (the content is scaled and the movie clip itself is not scaled)
BMD Example: 100*100px.
Note: Although the content in the target is only half the content in the source MC, the size of the copied content is still the size of the BMD instance.

Souce Source Contents: 100*100px (the movie clip itself is not scaled);
Dest2 the contents of the target clip: 100*100px (content not scaled, movie clip itself scaled 50*50)
BMD Example: 100*100px.
We see that the contents of the entire source have been fetched and copied to the target, and the image has been scaled because the target has been scaled.

Souce Source Contents: 100*100px (the movie clip itself is not scaled);
Dest2 the contents of the target clip: 50*50px (the content is scaled, the movie clip itself is scaled 200*200)
BMD Example: 100*100px.
The content of the target MC is reduced, the size of the target MC is scaled to 200*200, and the image of the BMD instance is found to be independent of the content size of the target, which is related to the size of the target MC.

If the size of the BMD instance is smaller than the content size in the source MC, only part of the content is copied, not the entire image.

The BMD instance content is less than the size of BMD and still gets the entire image.

Star 1 Example:
Star Example:

You can download the source file from here:stars.zip. We need to use it below.
In this example we will also use filter. You can read a tutorial Flash8 the use of Mealstrom basics.

After opening the source file, you look at the code in the as layer below:

Code:

var w:number = stage.width;
var h:number = stage.height;
var Star:movieclip;

Version detection Check
var Versionnums:array = $version. ToString (). Split ("");
Inform.text = (number (VERSIONNUMS[1].SUBSTR (0,1)) < 8)? "You need the Flash 8 player to view this movie!": "";

Create Children
var sourcemc:movieclip = This.createemptymovieclip ("Sourcemc", this.getnexthighestdepth ());
var destmc:movieclip = Sourcemc.createemptymovieclip ("DESTMC", sourcemc.getnexthighestdepth ());
var screenshot = new Flash.display.BitmapData (W, H, true, 0);
//-------------------------------------------|
Destmc.onenterframe = function ()
{
Drawstar ();
Capture ();
Drawtoscreen ();
}
//-------------------------------------------|
function Drawstar ()
{
Star = Sourcemc.attachmovie ("Star", "Star", 1); Attach at 1
Glow.color = Math.floor (Math.random () *0xffffff);
Star.filters = [Glow,bevel];
star._x = Getrandomint (W, (STAR._WIDTH/2));
Star._y = Getrandomint (h, (STAR._HEIGHT/2));
}
//-------------------------------------------|
function Capture ()
{
Destmc._visible = false;
Screenshot.draw (SOURCEMC);
Destmc._visible = true;
}
//-------------------------------------------|
function Drawtoscreen ()
{
Destmc.attachbitmap (screenshot, 1); Attach at 1
}
//-------------------------------------------|
function Getrandomint (max, sub): Number
{
Return Math.floor (Math.random () * (max+1)-(sub*2)) + sub);
}

I got the width and height of the screen, and dynamically created two MovieClip. One is SOURCEMC. The other is DESTMC. Then create an instance of BitmapData.
Next, I added some code in the Onenerframe method:
Code:

Destmc.onenterframe = function ()
{
Drawstar ();
Capture ();
Drawtoscreen ();
}

We draw a star on the stage, take a screenshot, and finally transfer that screenshot to a MovieClip.
We draw the star in the scene and pass it to a movieclip.
The following is the code for "draw":

Code:

function Drawstar ()
{
Attach star at depth 1
var star = Sourcemc.attachmovie ("Star", "Star", 1);
Glow.color = Math.floor (Math.random () *0xffffff);
Star.filters = [Glow,bevel];
star._x = Getrandomint (W, (STAR._WIDTH/2));
Star._y = Getrandomint (h, (STAR._HEIGHT/2));
}

Drawstar: (Drawing stars)

We actually pasted an image that was prepared before the library, and note that we used a fixed depth of pasting to be replaced when a new star repeats. The naming side of the source and target clips is useful. You will see why in the Capture () function.

The second line changes the color of the glow filter, and you can see in the code on the "filter" layer that we're using the Math.random () to create a random color between black and white.
In the third line, we specify the filter for the MC.
Finally using my Custom Function Getrandomint (), we set the X and Y positions to be a random integer, and within the screen border.
Capture: (buffering)

The following function will be very simple.

Code:

function Capture ()
{
Destmc._visible = false;
Screenshot.draw (SOURCEMC);
Destmc._visible = true;
}

Here we first let our target MC not visible, why? Because we are in the cycle of the target MC will contain a our previous primitive stars, we just need to use other stars, and I also want to do is to watch the bitmapdata work process.

The Draw method (used in the next function) does not erase bitmapdata. Instead of mixing the new data with the old ones, we don't want to see the effect of first screen, then empty, and then one screen. It should be noted that the stars are normal when using glow. Sometimes there may be abnormal phenomena around the glow.
The following are the effects you want:


The following may not be the effect you want to see:

Well, when we end our capture () function, we let DESTMC show it.

Drawtoscreen: (Painted to screen)

The function of the Drawtoscreen function is the same as its title. The goal is to draw it on the screen. The following code:

Code:

function Drawtoscreen ()
{
Destmc.attachbitmap (screenshot,1);
}

Finally, you can test your video.

Summary: In this tutorial every time I run hide the target MC, in addition to this, you can also do other operations, example good, replace Alpha,xscale.yscale and so on attributes, you can add other filter effects, such as blur, if you drag a vector graphics object, You can use Cacheasbitmap to make it easier.

If you want to erase the contents of BMD, you need to use FillRect (Rectangle,color); To do this, you need to use the Geom class to create a rectangle, see previous tutorials Flash8 Maelstrom basics
Roughly as follows:

Code:

var rect = new Flash.geom.Rectangle (0, 0, width, height);
Bmap.fillrect (rect, color);

Hopefully, there are some tutorials that will give you a good understanding of BitmapData class usage. Finish. enjoy!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.