艺虎动画 > flash AS3.0实例教程:flash制作动画踪迹效果

flash AS3.0实例教程:flash制作动画踪迹效果

翼虎动漫   2010/9/19

 

 

 

http://img.flashzhizuo.net.cn/us/201091914559202.swf

 

详细代码:

/**

*Flash CS4 ActionScript 3.0

*这里呈现粒子基本技术的一个动画实例。当它到处移动时,运动的粒子在它后面

*留下了一个彗星的痕迹。这是因为粒子正在绘制到从不被抹掉的一个位图。

*滤镜应用到位图导致旧的图像慢慢地褪色离开,新的图像在顶端上绘制。

*/

package {

        import flash.display.Sprite;

        import flash.display.Bitmap;

        import flash.display.BitmapData;

        import flash.events.Event;

        import flash.geom.Point;

        import flash.filters.BlurFilter;

        import flash.geom.ColorTransform;

        import flash.display.Shape;

    

        [SWF(width = "420", height = "320", backgroundColor = "0xffffff", frameRate = "30")];

        public class Main extends Sprite {

                private var particle:Sprite;

                private var bitmapData:BitmapData;

                private var bitmap:Bitmap;

                private var velX:Number;

                private var velY:Number;

                private var radius:Number;

                private var displayWidth:Number;

                private var displayHeight:Number;

                private var display:Sprite;

                private var origin:Point;

                private var blur:BlurFilter;

                private var colorTransform1:ColorTransform;

                private var colorTransform2:ColorTransform;

                private var randomAccel:Number;

                private var maxVelX:Number;

                private var maxVelY:Number;

                private var margin:Number;

                public function Main():void {

                        init();

                }

                private function init():void {

                        radius = 5;

                        margin = radius + 2;



                        particle = new Sprite();

                        particle.graphics.lineStyle(2,0xffffff);

                        particle.graphics.beginFill(0xffffff,0.5);

                        particle.graphics.drawEllipse(-radius,-radius,2 * radius,2 * radius);

                        particle.graphics.endFill();

                        //设定一个开始的位置

                        particle.x = 50;

                        particle.y = 40;

                        display = new Sprite();

                        display.addChild(particle);



                        //运动的参数

                        velX = 1;

                        velY = 1.3;

                        randomAccel = 0.3;

                        maxVelX = 6;

                        maxVelY = 6;



                        //位图的大小:

                        displayWidth = 400;

                        displayHeight = 300;



                        //见到的位图:

                        bitmapData = new BitmapData(displayWidth,displayHeight,true,0x00000000);

                        bitmap = new Bitmap(bitmapData);



                        //绘制边框

                        var frame:Shape = new Shape();

                        frame.graphics.lineStyle(1,0x333333);

                        frame.graphics.drawRect(-0.5,-0.5,displayWidth + 1,displayHeight + 1);



                        //BlurFilter滤镜

                        blur = new BlurFilter(4,4);

                        colorTransform1 = new ColorTransform(0.999,0.95,0.9,1);

                        origin = new Point(0,0);



                        /*

                        这里是颜色变换改为那一个在上面。它应用透明度衰减和颜色变化。改变代                                                     码在 onEnter 结束的时候动作,以便改为使用 colorTransform 2 。

                        */

                        colorTransform2 = new ColorTransform(0.999,0.95,0.9,0.999);



                        bitmap.x = frame.x = 10;

                        bitmap.y = frame.y = 10;

                        this.addChild(bitmap);

                        this.addChild(frame);



                        this.addEventListener(Event.ENTER_FRAME, onEnter);





                }

                function onEnter(evt:Event):void {

        

            //随机的加速。

            velX += randomAccel*(2 * Math.random()-1);

            velY += randomAccel*(2 * Math.random()-1);

        

            //

            if (velX > maxVelX) {

                   velX = maxVelX;

             }

        

             else if (velX < -maxVelX){

                   velX = -maxVelX;

             }

              if (velY > maxVelY) {

                    velY = maxVelY;

             }

            else if (velY < -maxVelY) {

                velY = -maxVelY;

            }

        

            // 移动粒子以及反弹粒子

            

            particle.x += velX;

            if (particle.x < margin) {

                particle.x = margin;

                velX *= -1;

            }

            else if (particle.x > displayWidth - margin) {

                  particle.x = displayWidth - margin;

                    velX *= -1;

            }

        

            particle.y += velY;

              if (particle.y < margin) {

                     particle.y = margin;

                     velY *= -1;

            }

            else if (particle.y > displayHeight - margin) {

                particle.y = displayHeight - margin;

                velY *= -1;

            }

        

            //在绘制新的图像之前应用滤镜到旧的图像。

            //粒子留下的踪迹从不抹掉,它仅仅逐渐地褪色离开。

            bitmapData.applyFilter(bitmapData,bitmapData.rect,origin,blur);

            bitmapData.colorTransform(bitmapData.rect, colorTransform1);

            bitmapData.draw(display);

        

        }

    }

}