//We need few imports for the color import fl.motion.Color; import flash.geom.ColorTransform; /*We want 20 particles at the start particlesArray is used when we animate each particle */ var numberOfParticles:Number = 20; var particlesArray:Array = new Array(); //Each time a hit occurs, we want to create 10 new particles var numberOfExplosionParticles:uint = 10; //This loop creates the first particles and gives them speed and coordinates for (var i=0; i < numberOfParticles; i++) { var particle:Particle = new Particle(); //We want the particles to stay at their original position particle.speedX = 0; particle.speedY = 0; //Set the starting position particle.y = Math.random() * stage.stageHeight; particle.x = Math.random() * stage.stageWidth; //Add the particle to the stage and push it to array for later use. addChild (particle); particlesArray.push (particle); } //Call for the first explosion startExplosions (); /*This function makes a random particle to explode. From here, the chain reaction begins.*/ function startExplosions ():void { //Select a random particle from an array var index = Math.round(Math.random() * (particlesArray.length-1)); var firstParticle:Particle = particlesArray[index]; //Set a random tint var ct:Color = new Color(); ct.setTint (0xFFFFFF * Math.random(),1); //Create 10 new particles because of explosion for (var i=0; i < numberOfExplosionParticles; i++) { var particle:Particle = new Particle(); /*Give random x and y speed to the particle. Math.random returns a random number n, where 0 <= n < 1. */ particle.speedX = Math.random()*10 - 5 ; particle.speedY = Math.random()*10 - 5; //Apply the randomly selected tint to each particle particle.transform.colorTransform = ct; //Set the starting position particle.y = firstParticle.y; particle.x = firstParticle.x; //Particle is part of an explosion particle.partOfExplosion = true; //Add the particle to the stage and push it to array for later use. addChild (particle); particlesArray.push (particle); } //Let's remove the particle that exploded (remove from stage and from the array) removeChild (firstParticle); particlesArray.splice (index,1); addEventListener (Event.ENTER_FRAME, enterFrameHandler); } //This function is responsible for the animation function enterFrameHandler (e:Event):void { //Loop through every particle for (var i=0; i < particlesArray.length; i++) { var particleOne:Particle = particlesArray[i]; //Update the particle's coordinates particleOne.y += particleOne.speedY; particleOne.x += particleOne.speedX; /*This loop calls a checkForHit function to find if the two particles are colliding*/ for (var j:uint = i + 1; j < particlesArray.length; j++) { var particleTwo:Particle = particlesArray[j]; /*Make sure the particles are on stage, only then check for hits*/ if (contains(particleOne) && contains(particleTwo)) { checkForHit (particleOne, particleTwo); } } } } /*This function checks whether two particles have collided*/ function checkForHit (particleOne:Particle, particleTwo:Particle):void { /*Let's make sure we only check those particles, where one is moving and the other is stationary. We don't want two moving particles to explode. */ if ((particleOne.partOfExplosion == false && particleTwo.partOfExplosion == true) || particleOne.partOfExplosion == true && particleTwo.partOfExplosion == false ) { //Calculate the distance using Pythagorean theorem var distanceX:Number = particleOne.x - particleTwo.x; var distanceY:Number = particleOne.y - particleTwo.y; var distance:Number = Math.sqrt(distanceX*distanceX + distanceY*distanceY); /* If the distance is smaller than particle's width, we have a hit. Note: if the particles were of different size, the calculation would be: distance < ((particleOne.width / 2) + (particleTwo.width / 2)) */ if (distance < particleOne.width) { //Set a random tint to the particles that explode var ct:Color = new Color(); ct.setTint (0xFFFFFF * Math.random(),1); //Create 10 new particles because of an explosion for (var i=0; i < numberOfExplosionParticles; i++) { var particle:Particle = new Particle(); particle.speedX = Math.random()*10 - 5 ; particle.speedY = Math.random()*10 - 5; //Apply tint particle.transform.colorTransform = ct; //Set the starting position particle.y = particleOne.y; particle.x = particleOne.x; particle.partOfExplosion = true; //Add the particle to the stage and push it to array for later use. addChild (particle); particlesArray.push (particle); } /* Check which of the two particles was stationary. We'll remove the one that was stationary. */ if (particleOne.partOfExplosion == false) { var temp1 = particlesArray.indexOf(particleOne); particlesArray.splice (temp1,1); removeChild (particleOne); } else { var temp2 = particlesArray.indexOf(particleTwo); particlesArray.splice (temp2,1); removeChild (particleTwo); } } } }