除了可以改变粒子的运动速度之外,还可以改变它的大小,通过设置它的growX和growY属性就可以达到目的。
package{ import flash.display.Sprite; import flash.events.Event; [SWF(width="800",height="600",backgroundColor="0xffffff",frameRate="31")] public class ParticleGrow extends Sprite{ private var numbers:Number = 20; private var particles:Array; public function ParticleGrow(){ particles=new Array(); stage.addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(e:Event):void{ if(particles.length < numbers){ var ball:Ball=new Ball(12, 0x00ff00, 1, 0x000000, 1); addChild(ball); ball.xVelocity=Math.random() * 20 - 10; ball.yVelocity=Math.random() * 20 - 10; ball.gravity=0.9; ball.growX = 1.05; ball.growY = 1.05; ball.x=stage.stageWidth/2; ball.y=stage.stageHeight/4; particles.push(ball); } for (var i:int=0; i < particles.length; i++){ var particle:Ball=particles[i]; particle.update(); if (particle.x - particle.radius > stage.stageWidth || particle.x + particle.radius < 0 || particle.y - particle.radius > stage.stageHeight || particle.y + particle.radius < 0){ particle.xVelocity=Math.random() * 20 - 10; particle.yVelocity=Math.random() * 20 - 10; particle.x=stage.stageWidth / 2; particle.y=stage.stageHeight / 4; particle.scaleX = particle.scaleY = 1; } } } } }
代码将ball粒子的growX和growY设置为1.05,从而实现逐帧放大的效果。当超过环境边界之后,再将它的scaleX和scaleY设置设置为1,恢复它原始的大小。