I added a chain reaction system to the circles popping. With all these new display objects on the screen, Ben-Fox (or anyone) could pull a another nifty effect using the get random object from array or stage.
Good job so far.
^_^
/* AS3 Code Collab! <http://www.newgrounds.com/bbs/topic/966360>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* A copy of the GNU General Public License is available at:
* <http://www.gnu.org/licenses/>.
*/
stage.frameRate = 30;
const CIRCLE_RADIUS_MIN:int = 25;
const CIRCLE_RADIUS_MAX:int = 50;
const CIRCLE_SPD:int = 7;
const CIRCLE_CREATION_DELAY:int = 500;
const GRAVITY:int = 5; //Gravity applied to particles ***Added by Calipe***
var maxParticles:int = 10; //Max number of particles
var maxCircles:int = 50;
var circles:Array = new Array(); //lets make a container so we can easily access the circles?
var circleCreationTimer:Timer = new Timer(CIRCLE_CREATION_DELAY, 0);
circleCreationTimer.addEventListener(TimerEvent.TIMER, createCircleEvent, false, 10, false);
circleCreationTimer.start();
function createCircleEvent(timerEvent:TimerEvent):void
{
var newCircle:Sprite = createCircleSprite();
newCircle.x = Math.random()*(stage.stageWidth - newCircle.width);
newCircle.y = - newCircle.height;
circles.push(newCircle); //Here we add the circles to the array
newCircle.addEventListener(Event.ENTER_FRAME, onCircleEnterFrame, false, 5, true);
newCircle.addEventListener(MouseEvent.MOUSE_OVER, mouseOverListener, false, 0, true); //Registers the circle for the mouse over event ***Added by Calipe***
addChild(circles[circles.length - 1]);
}
// original content **PrettyMuchBryce** ( modified )
function onCircleEnterFrame(event:Event):void
{
var currentCircle:Sprite = event.currentTarget as Sprite;
currentCircle.y += CIRCLE_SPD;
if ((currentCircle.y - currentCircle.height) > stage.stageHeight)
{
currentCircle.removeEventListener(Event.ENTER_FRAME, onCircleEnterFrame); // EXTREMELY IMPORTANT FOR GARBAGE COLLECTION
removeChild(currentCircle);
circles.splice(circles.indexOf(currentCircle), 1);
}
}
function createCircleSprite():Sprite
{
var radius:int = CIRCLE_RADIUS_MIN + Math.random()*(CIRCLE_RADIUS_MAX - CIRCLE_RADIUS_MIN);
var tempCircle:Sprite = new Sprite();
tempCircle.graphics.lineStyle();
tempCircle.graphics.beginFill(Math.random()*0xFFFFFF);
tempCircle.graphics.drawCircle(radius, radius, radius);
tempCircle.graphics.endFill();
return tempCircle;
}
// ** Ben-Fox ** random display object getter
function getRandomSprite(sourceArray:Array = null):Sprite {
if (sourceArray == null) { //If no array reference is passed to the function...
//Choose any child on the stage to return.
return getChildAt(Math.floor(Math.random() * numChildren)) as Sprite;
} else {
//Return a reference from the specified array
return sourceArray[Math.floor(Math.random() * sourceArray.length)] as Sprite; //Just in case
}
}
//The event listener function that is triggered when the mouse over event is dipatched ***Added by Calipe***
function mouseOverListener(event:MouseEvent):void
{
popCircle(Sprite(event.target), mouseX, mouseY);
}
// updated the circle pop, so mouse overs were not the only method for triggering them. ** deadlock32 **
function popCircle(targetCircle:DisplayObject, centerX:Number, centerY:Number):void
{
removeChild(targetCircle);
circles.splice(circles.indexOf(targetCircle), 1);
targetCircle.removeEventListener(Event.ENTER_FRAME, onCircleEnterFrame);
createParticles(centerX, centerY);
}
//Creates the particles ***Added by Calipe***
function createParticles(centerX:Number, centerY:Number):void
{
for (var i:int = 0; i < maxParticles; i++)
{
var particle:MovieClip = new MovieClip();
particle.graphics.beginFill(Math.random() * 0xFFFFFF);
particle.graphics.drawCircle(0, 0, Math.random() * 10 + 5);
particle.vx = Math.random() * 30 - 15;
particle.vy = Math.random() * -50;
particle.x = centerX;
particle.y = centerY;
particle.addEventListener(Event.ENTER_FRAME, particleEnterFrame, false, 0, true);
addChild(particle);
}
}
//Particles enter frame listener ***Added by Calipe***
function particleEnterFrame(event:Event):void
{
var tempParticle:MovieClip = MovieClip(event.target);
if ( tempParticle.y < stage.stageHeight)
{
checkIfParticlePopsCircle(tempParticle);
tempParticle.vy += GRAVITY;
tempParticle.x += tempParticle.vx;
tempParticle.y += tempParticle.vy;
}
else
{
removeChild(tempParticle);
tempParticle.removeEventListener(Event.ENTER_FRAME, particleEnterFrame, false);
}
}
// added a function to check if a particle is touching another circle
// by deadlock32
function checkIfParticlePopsCircle(targetParticle:DisplayObject):void
{
var targetParticleRadius:Number = targetParticle.width * 0.5;
for each(var tempCircle:DisplayObject in circles)
{
var circleRadius:Number = tempCircle.width * 0.5;
var collisionDistanceMax:Number = targetParticleRadius + circleRadius;
var distanceApart:Number = Point.distance(new Point(targetParticle.x, targetParticle.y), new Point(tempCircle.x, tempCircle.y));
if(distanceApart <= collisionDistanceMax)
{
popCircle(tempCircle, tempCircle.x, tempCircle.y);
break;
}
// the above was 100%... for some reason if you wanna figure out why go for it
// I added the below to help compensate
if(tempCircle.hitTestPoint(targetParticle.x, targetParticle.y, true))
{
popCircle(tempCircle, tempCircle.x, tempCircle.y);
break;
}
}
}