Hi!
I’ve been sitting with this code for way too long now and need some new perspective to help.
Watch out for a huuuge lump of code below :diss:
I’m trying to make an animation that with a few bouncy balls that on collision spawns 1 extra ball, and this i’ve managed and contained so no more balls are spawned when the population reaches 20, however i need to find a way to remove the balls, i’ve tried arrays, removeChild and pretty much all that i can think of, but no luck so far (i guess im not that familliar with Action scripting yet)
Below is my mess of a code for collision control, and yes it is a mess for now and i intend to clean it up as soon as i can find a way to remove the balls individually…
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.KeyboardEvent;
public class BallCollision extends MovieClip
{
private var mcBallContainer:MovieClip;
public var bacteria:Array = new Array();
public var nNumBalls:Number = 5;
public var nNumSpawned:Number = 5;
private var collision:uint= 0;
public var myString:String= String(nNumSpawned);
private var nStageWidth:Number = 500;
private var nStageHeight:Number = 400;
public function BallCollision ( ) : void
{
mcBallContainer = new MovieClip ( ) ;
mcBallContainer.x = mcBallContainer.y = 0;
stage.addChild(mcBallContainer);
this.addEventListener ( Event.ENTER_FRAME, enterFrameHandler );
for ( var i = 0; i < nNumBalls; i++ )
{
var mcBall:Ball = new Ball ( Math.floor ( ( Math.random() * 12 ) - 4 ), Math.floor ( ( Math.random() * 12 ) - 4 ) );
mcBall.x = Math.random() * nStageWidth;
mcBall.y = Math.random() * nStageHeight;
mcBallContainer.addChild ( mcBall );
}
}
private function enterFrameHandler ( E:Event ) : void
{
for ( var i = 0; i < mcBallContainer.numChildren; i++ )
{
var mcBall1:* = mcBallContainer.getChildAt( i );
for ( var j = i + 1; j < mcBallContainer.numChildren; j++ )
{
var mcBall2:* = mcBallContainer.getChildAt( j );
var nDistX:Number = Math.abs ( mcBall1.x - mcBall2.x );
var nDistY:Number = Math.abs ( mcBall1.y - mcBall2.y );
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
//TRIPLE IF BELOW = PROBLEM_1
if ( nDistance < 20)
{
solveBalls ( mcBall1, mcBall2 );
collision += 1;
collision_txt.text = String(collision);
if (nNumSpawned < 20){
var mcBall:Ball = new Ball ( Math.floor ( ( Math.random() * 12 ) - 4 ), Math.floor ( ( Math.random() * 12 ) - 4 ) );
mcBall.x = Math.random() * nStageWidth;
mcBall.y = Math.random() * nStageHeight;
mcBallContainer.addChild ( mcBall );
bacteria.push(mcBall);
nNumSpawned ++;
population_txt.text = nNumSpawned.toString();
trace (nNumSpawned);
trace (mcBallContainer.numChildren);
//REMOVES ALL CHILDREN? = PROBLEM_2
if (mcBallContainer.numChildren == 20){
trace ("Population Cap Reached");
bacteria.splice(Ball, 1);
trace (bacteria);
// code below Removes all "bouncy monsters"
//stage.removeChild(mcBallContainer);
// code below does absolutley nothing
//mcBallContainer.removeChild ( mcBall );
}
}
}
}
}
}
private function solveBalls ( MCBallA:MovieClip, MCBallB:MovieClip ) : void
{
var nX1:Number = MCBallA.x;
var nY1:Number = MCBallA.y;
var nDistX:Number = MCBallB.x - nX1;
var nDistY:Number = MCBallB.y - nY1;
var nDistance:Number = Math.sqrt ( nDistX * nDistX + nDistY * nDistY );
var nRadiusA:Number = MCBallA.width/2;
var nRadiusB:Number = MCBallB.width/2;
var nNormalX:Number = nDistX/nDistance;
var nNormalY:Number = nDistY/nDistance;
var nMidpointX:Number = ( nX1 + MCBallB.x )/2;
var nMidpointY:Number = ( nY1 + MCBallB.y )/2;
MCBallA.x = nMidpointX - nNormalX * nRadiusA;
MCBallA.y = nMidpointY - nNormalY * nRadiusA;
MCBallB.x = nMidpointX + nNormalX * nRadiusB;
MCBallB.y = nMidpointY + nNormalY * nRadiusB;
var nVector:Number = ( ( MCBallA.nSpeedX - MCBallB.nSpeedX ) * nNormalX )+ ( ( MCBallA.nSpeedY - MCBallB.nSpeedY ) * nNormalY );
var nVelX:Number = nVector * nNormalX;
var nVelY:Number = nVector * nNormalY;
MCBallA.nSpeedX -= nVelX;
MCBallA.nSpeedY -= nVelY;
MCBallB.nSpeedX += nVelX;
MCBallB.nSpeedY += nVelY;
}
}
}
If i run
trace (bacteria);
i get the following:
[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball],[object Ball]
now that should be all of the balls on the stage, however i cannot find a way to remove them one by one…
Sorry for that massive post, but i’m just way in over my head right now and need some perspective…