for(varj:uint=i+1;j<particlesArray.length;j++){
varparticleTwo:Particle=particlesArray[j];
/*Makesuretheparticlesareonstage,onlythencheckforhits*/
if(contains(particleOne)&&contains(particleTwo)){
checkForHit(particleOne,particleTwo);
}
}
}
}
/*Thisfunctioncheckswhethertwoparticleshavecollided*/
functioncheckForHit(particleOne:Particle,particleTwo:Particle):void{
/*Let’smakesureweonlycheckthoseparticles,whereoneismovingandtheother
isstationary.Wedon’twanttwomovingparticlestoexplode.*/
if((particleOne.partOfExplosion==false&&particleTwo.partOfExplosion==true)||
particleOne.partOfExplosion==true&&particleTwo.partOfExplosion==false){
//CalculatethedistanceusingPythagoreantheorem
vardistanceX:Number=particleOne.x-particleTwo.x;
vardistanceY:Number=particleOne.y-particleTwo.y;
vardistance:Number=Math.sqrt(distanceX*distanceX+distanceY*distanceY);
/*Ifthedistanceissmallerthanparticle’swidth,wehaveahit.
Note:iftheparticleswereofdifferentsize,thecalculationwouldbe:
distance<((particleOne.width/2)+(particleTwo.width/2))
*/
if(distance<particleOne.width){
//Setarandomtinttotheparticlesthatexplode
varct:Color=newColor();
ct.setTint(0xFFFFFF*Math.random(),1);
//Create10newparticlesbecauseofanexplosion
for(vari=0;i<numberOfExplosionParticles;i++){
varparticle:Particle=newParticle();
particle.speedX=Math.random()*10-5;
particle.speedY=Math.random()*10-5;
//Applytint
particle.transform.colorTransform=ct;
//Setthestartingposition
particle.y=particleOne.y;
particle.x=particleOne.x;
&nbs,p;particle.partOfExplosion=true;
//Addtheparticletothestageandpushittoarrayforlateruse.
addChild(particle);
particlesArray.push(particle);
}
/*Checkwhichofthetwoparticleswasstationary.
We’llremovetheonethatwasstationary.
*/
if(particleOne.partOfExplosion==false){
vartemp1=particlesArray.indexOf(particleOne);
particlesArray.splice(temp1,1);
removeChild(particleOne);
}
else{
vartemp2=particlesArray.indexOf(particleTwo);
particlesArray.splice(temp2,1);
removeChild(particleTwo);
}
}
}
}
附件下载:Particle.rar 粒子.rar










