Sorry if this has been covered, but I felt like doing a tutorial. As you may be aware, AS:Main is no longer updated, so this is just here for a direct link kinda thing.
Requirements:
- Flash 8 or later
- Basic understanding of MovieClips and stuff (creating MCs at runtime - its less complicated than it sounds :) )
Usage
This is basically a script to save a screen from your movie to a bitmapData variable. It doesn't have to be the whole screen - you can change the identifier to certain MCs or whatever.
It's usefull for stuff like game-over screenshots, user-taken screenshots, icons for saved games, and all kinds of things.
It can be a heavy bit of code, so it's best for rare events like game-over or certain key presses where the user isn't likely to notice one frame taking a few fractions of a second longer than it should.
The code
First, we import the necessary class and define the piece of bitmapData in which we will be holding the screenshot:
import flash.display.BitmapData;
var bmp1:BitmapData = new BitmapData(Stage.width, Stage.height);
The required parameters of the constructor are the width and height of the bitmap - for more imformation and the additional parameters, see the documentation for the BitmapData class.
Now, the easy part! Taking the screenshot itself. Once you have the bitmap object, you can tell it to draw everything in a certain instance (in this case, the main timeline) with one method:
bmp1.draw(_root);
A little explaination - the draw() method tells the BitmapData to convert all of the vector imformation in the target (the first parameters) into a bitmap in the target BitmapData.
The target isn't the only parameter. In full, they are:
BitmapData.draw(target, transform matrix, color transform, blend mode, clipping rectangle, smoothing (boolean));
I don't tend to use all of them. The color transform is nice, since it lets you play with the colors of the bitmap when you draw it. For example, to take a partially inverted screenshot, you could use:
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
var inver:ColorTransform = new ColorTransform(-0.6, -0.6, -0.6, 1, 200, 200, 200, 0);
var bitm:BitmapData = new BitmapData(Stage.width, Stage.height);
bitm.draw(_root, new Matrix(), inver);
Remember to import all of the classes you need. Also remember that to skip a parameter like Matrix, use its default constructor.
To display the bitmap, all you need to do is place it in a MovieClip and stick it where you want. So, assuming you already have your variable in place, you would use:
createEmptyMovieClip("bmp1hold", 1000);
bmp1hold.attachBitmap(bmp1, 1001);
bmp1hold._x = bmp1hold._y = 0;
This creates an empty clip at a depth of 1000, then attaches to it the bitmap we previously saved at a depth inside the clip of 1001. It then moves the clip to the origin.
Remember that you can scale and the MovieClip to position your Bitmap, if you don't want to mess around with Matrices. It probaly takes up more space to do it this way, so bear that in mind for large transformations.
Also remember that the clip's default depth will be above the rest of the stage. If you have any objects to display above the screenshot, such as custom cursors or frames, you must swap its depth to one greater than you have given your empty movie clip (in this case, above 1000):
cursor.swapDepths(15000);
bmpframe.swapDepths(9000);
Also remember that clips set to a positive depth will not be removed as the player needs their initial frames. If you want to remove your objects, you will have to use:
bmp1hold.removeMovieClip();
bmpframe.removeMovieClip();
Alternatively, if you need to remove a lot:
for(i in this){
if(typeof(this[i]) == "movieclip" && i != "cursor"){
this[i].removeMovieClip();
}
}
This will remove all movieclips of positive depth, other than "cursor".
That should be pretty much all you need to know. For any confusion, see documentation on the classes involved, or check back to AS:Main ;)
- Paranoia
example below