The current mainstream idea of Particle effect is bitmapData's setPixel method,
This method can set the color of the pixel in one position. The first two parameters are used to set the x and y coordinates of the positions,
The third parameter is to set the color value of the pixel. Let's look at the code and talk about more feel:
Package
{
Public class PixelVO
{
Public var xpos: Number;
Public var ypos: Number;
Public var speedX: Number;
Public var speedY: Number;
}
}
OK. Next we will create our main class. Please allow me to omit the import statements for some columns:
[SWF (width = "500", height = "450")]
Public class MyColorPicker extends Sprite
{
Private var pixelNum: int = 500;
Private var bm: Bitmap;
Private var BMI: BitmapData;
Private var container: Sprite;
Private var pixelList: Array = new Array ();
Private var gravity: Number = 0.1;
Public function MyColorPicker ()
{
Init ();
}
Private function init (): void {
Container = new Sprite ();
Container. x = 50;
Container. y = 0;
AddChild (container );
BMI = new BitmapData (400,400, true, 0xff000000 );
Bm = new Bitmap (BMI );
Container. addChild (bm );
Container. addEventListener (MouseEvent. CLICK, onClick );
}
Private function onClick (event: MouseEvent): void {
Fire (container. mouseX, container. mouseY );
}
Private function fire (toX: Number, toY: Number): void {
For (var I: int = 0; I <pixelNum; I ++)
{
Var vo: PixelVO = new PixelVO ();
Vo. xpos = toX;
Vo. ypos = toY;
Vo. speedX = 2-Math. random () * 4;
Vo. speedY = 1-Math. random () * 4;
PixelList. push (vo );
}
If (! This. hasEventListener (Event. ENTER_FRAME ))
{
This. addEventListener (Event. ENTER_FRAME, onEF );
}
}
Private function onEF (event: Event): void {
BMI. lock ();
For (var I: int = 0; I <pixelList. length; I ++)
{
Var item: PixelVO = pixelList [I] as PixelVO;
Item. xpos + = item. speedX;
Item. ypos + = item. speedY;
Item. speedY + = gravity;
BMI. setPixel (item. xpos, item. ypos, 0 xffffff );
If (item. ypos> 400 ){
PixelList. splice (I, 1 );
I --;
Item = null;
}
}
BMI. unlock ()
}
In the init () function, I originally wanted to put a bitmap directly onto the stage. However, bitmap cannot listen to mouse click events,
You can only add a Sprite container to the container. This container does not need to set its width and height, because Sprite can adjust its length and width based on its internal sub-object size.
I created a bitmap for displaying fireworks. When bitmapData is new, I set all four of its parameters. The first two are their sizes,
The third is to set whether the specified bitmap image supports different transparency of each pixel. The default value is true, and the fourth is the 32-bit ARGB color value used to fill the bitmap image area,
That is, it is similar to the 0xffffffff format. Generally, we only use six digits after the color value 0x. Here we use two more Alpha values to set transparency. Here we set the value 0xff000000,
Indicates that the transparency is the maximum, that is, the completely opaque black.
Then we set the click function and run the fire function. The fire function is used to generate fireworks. Once you hear the function name, you will find it very disappointing and the code in it is easy to understand,
Create pixelNum fireworks particles and set the initial position of each particle to a speed of mouse clicks and random. This speed can be left or right down.
Finally, the main difficulty lies in the enterFrame processing function. For
BMI. lock ();
BMI. unlock ();
The use of such a tag pair will directly affect the running efficiency. It is locked before processing the particles, and then rendered after the data processing is complete.
To improve the running efficiency, when someone asks you why you want to use these two sentences, you can say "no explanation ".
In the tag center, we traverse the particle array to process each particle, update the particle position, and make the speed on the Y axis affected by the acceleration of gravity.
Then, run the setPixel () method to set the color of the corresponding position of the particle. Here, the color is set to white.
In this way, the changes in the particle position will be displayed when each frame arrives. Finally, we move the particles at the bottom of the fireworks container from the array.
Run it. We found that the fireworks were released, but the path of every particle moving was still:
This is not what we want, but think about it. We used setPixel to set the color of a pixel before setting it back to the original color,
This situation is taken for granted. What should we do? We can find a class named ColorTransform, which can set the color value of an object and see its parameters first:
ColorTransform (redMultiplier: Number = 1.0, greenMultiplier: Number = 1.0, blueMultiplier: Number = 1.0, alphaMultiplier: Number = 1.0, redOffset: Number = 0, greenOffset: Number = 0, blueOffset: number = 0, alphaOffset: Number = 0)
This parameter is really TMD many, but we generally only use the first three here, they represent the red, green, and blue multiplier values, within the range of 0 to 1.
After these three parameters are set, you can multiply the red, green, and blue color values of the objects to be set the color to form a new color.
To reduce the color value in the particle motion path when each frame arrives, we set these three parameters to a decimal point smaller than 1.
Add the following statement to the statement:
Private var ctf: ColorTransform = new ColorTransform (0.9, 0.96, 0.96 );
In the onEF function, add:
BMI. lock ();
Bone mineral density. colorTransform (bone mineral density. rect, ctf );
The colorTransform attribute in bitMapData allows us to adjust the color value in the specified area of the bitmap image using the ColorTransform object.
Each frame uses a ColorTransform object with a red, green, and blue color multiplier less than 1 to pass the value to the BMI, and then the target of each frame is reduced to the target of the BMI.
The color of all pixels in the image is 0.9, 0.96, and 0.96, so that we can see that the current position of the particle is a normal color,
Because his color is newly set, and the color of the path that it has been moved is constantly fading, and finally it is integrated with the background color, because they are all set to black 0x000000.
To make the visual effect more beautiful, add a blur filter:
All code
Private var bf: BlurFilter = new BlurFilter (6, 6, 2 );
......
Private function onEF (event: Event): void {
BMI. lock ();
Bone mineral density. colorTransform (bone mineral density. rect, ctf );
> );
......