淡出粒子

二维粒子 everyinch 5635℃ 0评论

为了实现粒子在视觉上消失的效果,通过设置Particle的fade属性为0~1之间的小数,使它的alpha逐渐降低,从而达到淡出粒子的目的。

1package{
2    import flash.display.Sprite;
3    import flash.events.Event;
4 
5    [SWF(width="800",height="600",backgroundColor="0xffffff",frameRate="31")]
6    public class ParticleFade extends Sprite{
7        private var numbers:Number = 20;
8        private var particles:Array;
9 
10        public function ParticleFade(){
11            particles=new Array();
12            stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
13        }
14 
15        private function onEnterFrame(e:Event):void{
16            if(particles.length < numbers){
17                var ball:Ball=new Ball(12, 0x00ff00, 1, 0x000000, 1);
18                addChild(ball);
19                ball.xVelocity=Math.random() * 20 - 10;
20                ball.yVelocity=Math.random() * 20 - 10;
21                ball.gravity=0.9;
22                ball.growX = 1.05;
23                ball.growY = 1.05;
24                ball.fade = 0.98;
25                ball.x=stage.stageWidth/2;
26                ball.y=stage.stageHeight/4;
27                particles.push(ball);
28            }
29            for (var i:int=0; i < particles.length; i++){
30                var particle:Ball=particles[i];
31                particle.update();
32                if (particle.x - particle.radius > stage.stageWidth || particle.x + particle.radius < 0 || particle.y - particle.radius > stage.stageHeight || particle.y + particle.radius < 0){
33                    particle.xVelocity=Math.random() * 20 - 10;
34                    particle.yVelocity=Math.random() * 20 - 10;
35                    particle.x=stage.stageWidth / 2;
36                    particle.y=stage.stageHeight / 4;
37                    particle.scaleX = particle.scaleY = 1;
38                    particle.alpha = 1;
39                }
40            }
41        }
42    }
43}

在初始化粒子的循环中将粒子的fade属性设置为0.98,在遍历粒子数组并使其运动的循环中调用update函数使粒子运动并改变它的alpha属性。超过环境边界后,再重置它的速度和位置,并将它的alpha设置回1,从而在下一次的遍历循环中演示它的fade属性。

分享&收藏

转载请注明:陈童的博客 » 淡出粒子

喜欢 (1)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 让客户满意放心,才是生意的硬道理。
'; } if( dopt('d_footcode_b') ) echo dopt('d_footcode'); ?>