flash as3 3d烟花效果实现代码

 代码如下 复制代码
package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix3D;
    import flash.geom.PerspectiveProjection;
    import flash.geom.Point;
    import flash.geom.Utils3D;
    import flash.geom.Vector3D;
 
    [SWF(width = 465, height = 465, backgroundColor = 0x0, frameRate = 30)]
    /**
     * ...
     * @author lizhi
     */
    public class Fireworks3D extends Sprite {
        private var cm:Matrix3D = new Matrix3D();
        private var cvm:Matrix3D = new Matrix3D();
        private var m:Matrix3D = new Matrix3D();
        private var vm:Matrix3D = new Matrix3D();
        private var p:PerspectiveProjection = new PerspectiveProjection();
        private var pm:Matrix3D;
        private var vins:Vector.<Number> = new Vector.<Number>();
        private var vouts:Vector.<Number> = new Vector.<Number>();
        private var uvts:Vector.<Number> = new Vector.<Number>();
        private var v2ds:Vector.<Number> = new Vector.<Number>();
 
        private var view2:Sprite = new Sprite();
        private var view:Shape = new Shape();
        private var bmd:BitmapData = new BitmapData(465, 465, true, 0);
        private var particles:Array = [];
 
        public function Fireworks3D() {
            pm = p.toMatrix3D();
            addEventListener(Event.ENTER_FRAME, render);
            stage.addEventListener(MouseEvent.CLICK, onClick);
            addChild(view);
            //addChild(new Bitmap(bmd));
            view.x = 228;
            view.y = 228;
        }
 
        private function onClick(e:Event=null):void {
            explode((Math.random()-0.5)*400, (Math.random()-0.5)*400, (Math.random()-0.5)*400, 0xffffff*Math.random());
        }
 
        private var t:int = 0;
 
        private function render(e:Event):void {
            if (Math.random()<0.05) {
                onClick();
            }
            cm.identity();
            cm.appendTranslation(0, 200, -550);
            cm.appendRotation(view.mouseX/10, Vector3D.Y_AXIS,cm.position);
            cm.appendRotation(-view.mouseY/10, Vector3D.X_AXIS,cm.position);
            cvm.rawData = cm.rawData;
            cvm.invert();
            vm.rawData = m.rawData;
            vm.append(cvm);
            view.graphics.clear();
            for (var i:int = particles.length - 1; i >= 0; i--){
                var p:particle = particles[i];
                p.vx *= p.deceleration;
                p.vy *= p.deceleration;
                p.vz *= p.deceleration;
                p.vy += p.gravity;
                p.position.x = p.position.x + p.vx;
                p.position.y = p.position.y + p.vy;
                p.position.z = p.position.z + p.vz;
                p.energy *= p.deceleration;
                if (p.energy < 0.05){
                    particles.splice(i, 1);
                } else {
                    p.vposition = vm.transformVector(p.position);
                    p.v2d = Utils3D.projectVector(pm, p.vposition);
                    view.graphics.lineStyle(400 / p.v2d.w, p.color);
                    view.graphics.moveTo(p.v2d.x, p.v2d.y);
                    var c:int = 0;
                    var tc:int = 4;
                    for (var j:int = p.tails.length - 1; j >= 0; j--,c++) {
                        if (c>p.energy*tc) {
                            p.tails.splice(j, 1);
                            continue;
                        }
                        var v:Vector3D = p.tails[j];
                        var vp:Vector3D = vm.transformVector(v);
                        var v2d:Vector3D = Utils3D.projectVector(pm, vp);
                        view.graphics.lineStyle(500 / p.v2d.w, p.color,(p.energy*tc-c)/p.energy*tc);
                        view.graphics.lineTo(v2d.x, v2d.y);
                    }
                    if (p.tails[0]) {
                        v = p.tails[p.tails.length - 1];
                        if (Math.sqrt((v.x-p.position.x)*(v.x-p.position.x)+(v.y-p.position.y)*(v.y-p.position.y)+(v.z-p.position.z)*(v.z-p.position.z))>p.length) {
                            p.tails.push(p.position.clone());
                        }
                    }else {
                        p.tails.push(p.position.clone());
                    }
                }
            }
            //bmd.colorTransform(bmd.rect, new ColorTransform(0.095, 0.095, 0.095, 0.095));
            //bmd.applyFilter(bmd, bmd.rect, new Point, new BlurFilter());
            //bmd.draw(view2);
        }
 
        private function explode(x:Number, y:Number, z:Number, color:uint):void {
            var c:int = 400*Math.random();
            while (c-- > 0){
                var p:particle = new particle;
                p.energy = Math.random() * 5;
                var m:Matrix3D = new Matrix3D();
                m.appendTranslation(0, 0,(Math.random()*.2+0.8) * 5);
                m.appendRotation(360 * Math.random(), Vector3D.X_AXIS);
                m.appendRotation(360 * Math.random(), Vector3D.Y_AXIS);
                var v:Vector3D = m.transformVector(new Vector3D);
                p.vx = v.x;
                p.vy = v.y;
                p.vz = v.z;
                p.position = new Vector3D(x, y, z);
                p.color = color;
                p.deceleration = 0.95;
                p.gravity = 0.05;
                p.length = 2;
                particles.push(p);
            }
        }
    }
}
 
import flash.geom.Vector3D;
 
class particle {
    public var tails:Array = [];
    public var vx:Number;
    public var vy:Number;
    public var vz:Number;
    public var position:Vector3D;
    public var vposition:Vector3D;
    public var v2d:Vector3D;
    public var energy:Number;
    public var color:uint;
    public var deceleration:Number;
    public var gravity:Number;
    public var length:Number;
}
时间: 2024-09-21 01:10:58

flash as3 3d烟花效果实现代码的相关文章

flash as3.0的拖动物体代码

问题描述 flash as3.0的拖动物体代码 flash as3.0如何实现鼠标长按左键拖动影片剪辑,位置发生变化,松开后影片剪辑位置静止.(就像在win桌面上拖动一个文件夹到另一个位置 解决方案 Silverlight 2.0 PK Flash AS3.0Flash as3.0创建右键菜单[转]Flash.Flex.AS3.0框架及类库- 解决方案二: http://blog.sina.com.cn/s/blog_4909f3cb0100kkii.html 解决方案三: flash马上就要被

flash as3.0粒子效果实例教程

1.新建Flash文档,设置:宽.高为 400 × 400 ,保存. 2.用椭圆工具在舞台上画一个 20 × 20 大小的圆. (你能选择任意的颜色) 3.右键单击圆形,把它转换成影片剪辑,注册点居中. 4.在ActionScript导出的复选框中打勾 ,做类链接,类名为" Particle " .图1:5.把圆形从舞台删除,新建ActionScript 3.0文件.图2:6.我们编写一个外部的Particle类.在编译器中输入代码: package {         import 

flash as3 打气球小游戏代码

效果如下 首先是源代码,保存为main.as文件,关联到Fla即可运行.  代码如下   package {  import flash.display.*;  import flash.events.*;  import flash.text.*;  import flash.utils.Timer;  import flash.geom.Matrix;  [SWF(width=800, height=465,backgroundColor=0xffffff, frameRate=24)]  

flash as3 遮罩层效果

 代码如下 复制代码 package {     import flash.display.Bitmap;     import flash.display.BitmapData;     import flash.display.BitmapDataChannel;     import flash.display.Shape;     import flash.display.Sprite;     import flash.display.StageAlign;     import fl

JS仿flash上传头像效果实现代码_javascript技巧

先看效果图(在FF下): 下面是我整个的思路: 一.用ajaxFileUpload.js异步上传图片.这个不用多说了,挺简单的东西. 二.div拖拽用jquery-ui.这个也不多说了. 三.上传过图片的处理. 编辑容器是300*300,如果图片不在300*300之内,这里会生成一个maxHeight=300,maxWidth=300的缩略图, 用于设置编辑容器的背景,如果原文件为123.jpg,这里生成的文件为123.jpg.view.jpg,当然,文件123.jpg.view.jpg可能不会

flash as3实现打字小游戏代码

  游戏效果 新建Fla,在第一帧F9添加代码即可.  代码如下   var arr:Array = new Array(); var time:uint=0; var angle:Number=0; var zfun; [SWF(width=800, height=465,backgroundColor=0xffffff, frameRate=24)] this.addEventListener(Event.ENTER_FRAME,function(event){  time++;  angl

flash as3获取星期几多种实例代码

以下代码以AS3为例,当然,其他语言也基本适用.我们看一个简单的问题,如何获取当前是星期几? A程序员说,太简单了,我来写一段:  代码如下 复制代码 var date:Date = new Date; if (date.day == 0) {  trace("星期天"); } else if (date.day==1) {  trace("星期一"); } else if (date.day==2) {  trace("星期二"); } els

flash as3.0 Array 数组实例代码

 function testarray(){    var temp:Array = new Array(0,1,2);    var i:int = 0;       while( i<3 ){     if( i % 2 ){      trace( typeof temp[i]+"n" );     }else{      trace("好像不对戏!");     }     i++;    }       }      public function

flash as3.0加载swf失败解决方法

flash as3.0加载swf代码怎么写?如已有1.swf,用2.fla加载,在2.fla里的代码怎么写 补充:2.fla里已有一段,加载1.swf是想放完2.fla前一段后接着放2.swf的.所以代码是要写到2.fla本身的最后一帧的吧   var my_urlloader:loader=new loader(); my_urlloader.load(new urlrequest("swf.swf")); my_urlloader.contentloaderinfo.addeven