varnumberOfParticles:Number=20;
varparticlesArray:Array=newArray();
//Eachtimeahitoccurs,wewanttocreate10newparticles
varnumberOfExplosionParticles:uint=10;
//Thisloopcreatesthefirstparticlesandgivesthemspeedandcoordinates
for(vari=0;i<numberOfParticles;i++){
varparticle:Particle=newParticle();
//Wewanttheparticlestostayattheiroriginalposition
particle.speedX=0;
particle.speedY=0;
//Setthestartingposition
particle.y=Math.random()*stage.stageHeight;
particle.x=Math.random()*stage.stageWidth;
//Addtheparticletothestageandpushittoarrayforlateruse.
addChild(particle);
particlesArray.push(particle);
}
9、测试你的影片,效果如图。图4:
10、随机地选择一个粒子产生爆炸效果。爆炸后,生成新的粒子。最后,删除舞台上爆炸的粒子。把下列代码块加入到动作面板:
//CallforthefirstexplosionstartExplosions();
/*Thisfunctionmakesarandomparticletoexplode.
Fromhere,thechainreactionbegins.*/
functionstartExplosions():void{
//Selectarandomparticlefromanarray
varindex=Math.round(Math.random()*(particlesArray.length-1));
varfirstParticle:Particle=particlesArray[index];
//Setarandomtint
varct:Color=newColor();
ct.setTint(0xFFFFFF*Math.random(),1);
//Create10newparticlesbecauseofexplosion
for(vari=0;i<numberOfExplosionParticles;i++){
varparticle:Particle=newParticle();
/*Giverandomxandyspeedtotheparticle.
Math.randomreturnsarandomnumbern,where0<=n<1.*/
particle.speedX=Math.random()*10-5;
particle.speedY=Math.random()*10-5;
//Applytherandomlyselectedtinttoeachparticle
particle.transform.colorTransform=ct;
//Setthestartingposition
particle.y=firstParticle.y;
particle.x=firstParticle.x;
//Particleispartofanexplosion
particle.partOfExplosion=true;
//Addtheparticletothestageandpushittoarrayforlateruse.
addChild(particle);
particlesArray.push(particle);
}
//Let’sremovetheparticlethatexploded(removefromstageandfromthearray)
removeChild(firstParticle);
particlesArray.splice(index,1);
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
11、添加方法 enterFrameHandler,更新粒子坐标,使粒子动起来。输入下列代码:
functionenterFrameHandler(e:Event):void{
//Loopthrougheveryparticle
for(vari=0;i<particlesArray.length;i++){
varparticleOne:Particle=particlesArray[i];
//Updatetheparticle’scoordinates










