Rendering Buffer zone. I would like to explore some buffer filling technologies today. In fact, it is not a technology. We can understand it as painting. We open a piece of paper and fill the screen with what we want. First, let me know that as3.0 provides bitmapdata and graphics classes, which provide us with some bitmap data operations.
| |
|
Okay. Let's see how to implement bitmap filling. |
Create a new class named map class. The map class inherits the sprite class and serves as its subclass.
Package
{
Import flash. display. Sprite;
Import flash. display .*;
Import flash. Events .*;
Import flash. Geom .*;
Public class map extends Sprite
{
}
}
Next, add the constructor package.
{
Import flash. display. Sprite;
Import flash. display .*;
Import flash. Events .*;
Import flash. Geom .*;
Public class map extends Sprite
{
Public Function Map (mapdata: bitmapdata, MapX: Number, mapy: Number)
{
}
}
}
For a simple analysis, this map class includes map data, map data, map coordinates, and width. In this class, we introduce a method for creating graphs. Public Function createmap (bufferwidth: Number, bufferheight: Number): The Void method has three parameters. bufferwidth refers to the buffer width, bufferheight refers to the buffer height, and what we do, including creating a Rendering Buffer and specifying its size, such as buffer = new bitmapdata (bufferwidth, bufferheight, false, 0 xffffffff ); // set the buffer so that a filled buffer can be specified.Copy pixels to the buffer zone:
By
In the buffer zone
The above domain does not have any information data. In order to make the buffer have content, we use the method of copying pixels to copy the pixels we need. He is like a "back". The above words can be seen as our views.
Box, and the following is a basemap. Every time we move the image, we copy only the area we see, and the filled area is only the size of the buffer we set. This approach is conducive to our memory usage
.
For example:
Buffer. copypixels (mapdata, new rectangle (rect. x, rect. y, bufferwidth, bufferheight), new point (); // The coordinate change of the rectangle will crop different bitmap data.
Take a look at the complete code:
Public Function createmap (bufferwidth: Number, bufferheight: Number): void
{
Buffer = new bitmapdata (bufferwidth, bufferheight, false, 0 xffffffff); // set the buffer
Buffer. fillrect (New rectangle (0, 0, bufferwidth, bufferheight), 0x0000ff00); // fill the buffer
Trace (rect. y );
If (rect. x <0)
{
Rect. x = 0;
}
Trace (mapwidth );
If (rect. x + buffer. width> mapwidth)
{
Rect. x = mapWidth-buffer.width;
}
If (rect. Y <= 0)
{
Rect. Y = 0;
}
If (rect. Y + buffer. Height> mapheight)
{
Rect. Y = mapHeight-buffer.height;
}
Buffer. copypixels (mapdata, new rectangle (rect. x, rect. y, bufferwidth, bufferheight), new point (); // The coordinate change of the rectangle will crop different bitmap data.
Fillmap (buffer); // fill in bitmap data
}
The moving area cannot exceed the width and height of the basemap.Fill bitmap
The buffer zone cannot be displayed in our window. to display our window, we also need to fill the data in the window. We use some simple methods provided by the graphics class package. For example
Public Function fillmap (data: bitmapdata): void
{
Sprite. Graphics. Clear ();
Sprite. Graphics. beginbitmapfill (data, null, false, false );
Sprite. Graphics. drawrect (0, 0, Data. Width, Data. Height );
Sprite. Graphics. endfill ();
Addchild (sprite );
}
Complete code: Package
{
Import flash. display. Sprite;
Import flash. display .*;
Import flash. Events .*;
Import flash. Geom .*;
Public class map extends Sprite
{
Private var mapwidth: Number; // map width
Private var mapheight: Number; // map height
Private var MapX: Number; // X coordinate of the map
Private var mapy: Number; // y coordinate of the map
Private var maparray: array;
Private var mapdata: bitmapdata;
Public var sprite: SPRITE = new sprite (); // used as a container
Public var rect: rectangle; // rectangular shape of the map
Public var bitmap: bitmap;
Private var Buffer: bitmapdata; // Buffer
Public Function Map (mapdata: bitmapdata, MapX: Number, mapy: Number)
{
This. mapdata = mapdata;
// This. MapX = MapX;
// This. mapy = mapy;
This. mapwidth = mapdata. width;
This. mapheight = mapdata. height;
Bitmap = new Bitmap (mapdata );
Rect = bitmap. getrect (Bitmap); // obtain the rectangle of the map
}
Public Function get Mapinfo (): bitmapdata
{
Return mapdata;
}
Public Function createmap (bufferwidth: Number, bufferheight: Number): void
{
Buffer = new bitmapdata (bufferwidth, bufferheight, false, 0 xffffffff); // set the buffer
Buffer. fillrect (New rectangle (0, 0, bufferwidth, bufferheight), 0x0000ff00); // fill the buffer
Trace (rect. y );
If (rect. x <0)
{
Rect. x = 0;
}
Trace (mapwidth );
If (rect. x + buffer. width> mapwidth)
{
Rect. x = mapWidth-buffer.width;
}
If (rect. Y <= 0)
{
Rect. Y = 0;
}
If (rect. Y + buffer. Height> mapheight)
{
Rect. Y = mapHeight-buffer.height;
}
Buffer. copypixels (mapdata, new rectangle (rect. x, rect. y, bufferwidth, bufferheight), new point (); // The coordinate change of the rectangle will crop different bitmap data.
Fillmap (buffer); // fill in bitmap data
}
Public Function fillmap (data: bitmapdata): void
{
Sprite. Graphics. Clear ();
Sprite. Graphics. beginbitmapfill (data, null, false, false );
Sprite. Graphics. drawrect (0, 0, Data. Width, Data. Height );
Sprite. Graphics. endfill ();
Addchild (sprite );
}
}
}
|
Copypixels (Sourcebitmapdata: bitmapdata , Sourcerect: rectangle , Destpoint: Point , Alphabitmapdata: bitmapdata = NULL, alphapoint: Point = NULL, mergealpha: Boolean = False): void Provides a fast routine for Pixel processing between images without stretching, rotating, or color effects. |
After filling, add it to the display list to see the bitmap we need.
Drawrect