主页 > 知识库 > 平面设计 > Flash >

Flash 8制作烟花粒子的特效代码

来源: 作者: 发表于:2009-09-29 15:15  点击:
我这个Flash烟花粒子特效的制作其实很简单。 第一步:在Flash中建立画布550*400(宽度随便设的,高度应在200以上,因为烟花显
我这个Flash烟花粒子特效的制作其实很简单。

  第一步:在Flash中建立画布550*400(宽度随便设的,高度应在200以上,因为烟花显示的高度是200,这个也可以在代码里修改,代码第六行 var rect:Rectangle = new Rectangle(0, 0, Stage.width, 200); 这个200就是高度)
  第二步:在空白帧上点右键-动作 输入以下Action Script代码。

Stage.scaleMode = "noScale";
import flash.display.BitmapData;
import flash.filters.ConvolutionFilter;
import flash.geom.Rectangle;
var con = new ConvolutionFilter(3, 3, [1, 1, 1, 1, 30, 1, 1, 1, 1], 38);
var rect:Rectangle = new Rectangle(0, 0, Stage.width, 200);
var bitmap1 = new BitmapData(rect.width, rect.height, false,0x00000000);
var bitmap2 = new BitmapData(rect.width, rect.height, true,0x00000000);
var bitmapShow1 = createEmptyMovieClip("bitmapShow1", 1).attachBitmap(bitmap1, 1);
//粒子的显示画布
var bitmapShow2 = createEmptyMovieClip("bitmapShow2", 2).attachBitmap(bitmap2, 2);
//白色闪光点的显示画布
var m_mouseX;//鼠标x坐标
var m_mouseY;//鼠标y坐标
var sound1 = new Sound();
//上升过程的声音
var sound2 = new Sound();//爆炸声音
var bit_px = new Array(bits);//爆炸过程的x坐标
var bit_py = new Array(bits);//爆炸过程的y坐标
var bit_vx = new Array(bits);//爆炸过程的x速度
var bit_vy = new Array(bits);//爆炸过程的y速度
var bit_sx = new Array(bits);//上升过程的x坐标
var bit_sy = new Array(bits);//上升过程的y坐标
var bit_l = new Array(bits);//粒子的生命时间
var bit_f = new Array(bits);//是上升还是爆炸的标志
var bit_p = new Array(bits);//随机出现白色闪光点的标志
var bit_c = new Array(bits);//粒子颜色
init();
function init() {
 sound1.attachSound("sound1");
 sound2.attachSound("sound2");
 bits = 500;//总的最大粒子数
 bit_max = 50;//一次出现的粒子数
 bit_sound = 2;
 for (var j = 0; j<bits; j++) {
  bit_f[j] = 0;
 }
}
onEnterFrame = function () {
 bitmap2.fillRect(new Rectangle(0, 0, rect.width, rect.height), 0x00000000);
//白色闪光点清除
//bitmap2.applyFilter(bitmap, bitmap.rectangle, new Point(0, 0), con);
 rend();
 bitmap1.applyFilter(bitmap1, bitmap1.rectangle, new Point(0, 0), con);
//模糊滤镜
};
onMouseDown = function () {
 m_mouseX = _root._xmouse;
 m_mouseY = _root._ymouse;
 if (rect.contains(m_mouseX, m_mouseY)) {
  var k = int(Math.random()*256);
  var l = int(Math.random()*256);
  var i1 = int(Math.random()*256);
  var j1 = k << 16 | l << 8 | i1;
  var k1 = 0;
  for (var l1 = 0; l1<bits; l1++) {
   if (bit_f[l1] != 0) {
   continue;
  }
 bit_px[l1] = m_mouseX;
 bit_py[l1] = m_mouseY;
 var d = Math.random()*6.28;
 var d1 = Math.random();
 bit_vx[l1] = Math.sin(d)*d1/2;
 bit_vy[l1] = Math.cos(d)*d1/2;
 bit_l[l1] = int(Math.random()*100)+100;
 bit_p[l1] = int(Math.random()*3);
 if (random(4) == 0) {
  bit_c[l1] = 0xFFFFFF;
 } else {
  bit_c[l1] = j1;
 }
 bit_sx[l1] = m_mouseX;
 bit_sy[l1] = rect.height-5;
 bit_f[l1] = 2;
 if (++k1 == bit_max) {
  break;
 }
}
sound1.start(0);
}
};
function rend() {
 var flag:Boolean = false;
 var flag1:Boolean = false;
 var flag2:Boolean = false;
  for (var k = 0; k<bits; k++) {
   switch (bit_f[k]) {
   case 1 : // 爆炸过程
    bit_vy[k] += Math.random()/100;
    bit_px[k] += bit_vx[k];
    bit_py[k] += bit_vy[k];
    bit_l[k]--;
    if (bit_l[k] == 0 || !rect.contains(bit_px[k], bit_py[k])) {
     bit_c[k] = 0x000000;
     bit_f[k] = 0;
    } else if (bit_p[k] == 0) {
     if (int(Math.random()*2) == 0) {
      bit_setwhite(int(bit_px[k]), int(bit_py[k]), 0xFFFFFFFF);
     }
     bit_set(int(bit_px[k]), int(bit_py[k]), int(bit_c[k]));
    } else {
     bit_set(int(bit_px[k]), int(bit_py[k]), int(bit_c[k]));
    }
    break;
   case 2 :  // 上升过程
    bit_sy[k] -= 5;
    if (bit_sy[k]<=bit_py[k]) {
     bit_f[k] = 1;
     flag2 = true;
    }
    if (int(Math.random()*20) == 0) {
     var i = int(Math.random()*2);
     var j = int(Math.random()*5);
     bit_set(bit_sx[k]+i, bit_sy[k]+j, 0xFFFFFF);
    }
    break;
   }
  }
  if (flag2) {
   sound2.start(0);
  }
 }
 function bit_set(i:Number, j:Number, k:Number) {
 bitmap1.setPixel(i, j, k);
}
function bit_setwhite(i:Number, j:Number, k:Number) {
 bitmap2.setPixel32(i, j, k);//白色闪光点
}
 

  最后测试影片。

    有帮助
    (1)
    5.3%
    没帮助
    (18)
    94.7%