Bouncing_bubbles.fla this is the source code.
-
//allright people here it goes…
-
-
var count = 0;
-
-
//this is the variable that increments each time we add a
-
//new ball on the screen. Moreover we also decrement it every time we remove a ball
-
//to keep the Array balls coherent otherwise we would get undefined values
-
//if we decide to remove some of the balls and then add others.
-
-
var balls = new Array(); //this is the array that is going to keep the instance names of each ball on the stage var speedx = new Array();// this is the array that will contain the speeds on the X axis
-
-
var speedy = new Array(); // speedy = same thing as the speedx -> addBall below function adds movieclips on the stage and also creates instance names for each added ball. All the instance name are in the balls Array;
-
-
addBall.onRelease = function () {
-
-
balls[count]=_root.attachMovie("Ball", "ball"+count,_root.getNextHighestDepth());
-
-
//the above line places each instance name into the array
-
-
balls[count]._x=20+Math.random()*400; // these two lines place the movieclip on the stage at
-
-
balls[count]._y=20+Math.random()*200; // the designated coordinates with randomness.
-
-
speedx[count] = Math.random() * 8; //every time a ball is created a speed value is assigned to it on x or y axis
-
-
speedy[count] = Math.random() * 8; //every time a ball is created a speed value is assigned to it on x or y axis
-
-
color = new Color(balls[count]);
-
-
color.setRGB(Math.random()*0xFF0000+0x00FF00+0x0000FF);
-
-
trace(balls); // this traces the array
-
-
trace (count); // display that count value
-
-
speedx[balls.length-1].num = balls.length – 1;
-
-
speedy[balls.length-1].num = balls.length – 1;
-
-
balls[balls.length-1].num = balls.length – 1;
-
-
// in the above line we add the .num property equal with count to use it to maintain the array coherent after adding/removing balls
-
-
balls[balls.length-1].onRelease = function(){
-
-
//this part gives each ball the functionality to be clicked and to be removed off the stage.
-
//Also the names from the array are going to be removed and kept synchronized.
-
-
for (i=this.num+1; i balls[i].num–;}//this sets the nums of the balls
-
-
//in higher elements of the array. This part decrements the .num property to every element to the right
-
//of the clicked ball in the array. Example [1,2,3,4,5] if we click on 3 and remove it the
-
// elements to the right of it will have different position tags. so we have to decrement those.
-
-
speedx.splice(this.num,1); //this removes the value from the speedx array
-
-
speedy.splice(this.num,1); // speedy = the same as for speedx
-
-
balls.splice(this.num,1); // this line removes the element from the array
-
-
this.removeMovieClip(); //this line removes the actual movieclip
-
-
trace(speedx); // I am doing a trace here to see if the array is clean
-
-
trace(balls); // traces the array balls
-
-
count–; //decrements the counter to help keep the array synchronized after
-
-
//we removed a ball from the array. because if we then decide to add the empty
-
//spots in the array will appear as undefined.
-
-
}
-
-
trace(count);
-
-
trace(balls);
-
-
trace(balls[count].num);
-
-
speedx[count] = Math.random() * 8;
-
-
speedy[count] = Math.random() * 8;
-
-
realspeedx[count] = speedx[count];
-
-
realspeedy[count] = speedy[count];
-
-
count++;// this increments the counter.. don’t be fulled about the count. You would
-
-
//think that it should not be decremented before we increment but this is about the scope.
-
-
// we decrement only when we click to remove a ball.
-
-
}
-
-
//———————Movement—————-
-
-
var bounce = setInterval (movement, 30);// this interval calls the the movement function by an interval of 30 milliseconds or so
-
-
var moving:Boolean = true; // we need this value to test if the balls are moving or not
-
-
stopBtn.onRelease = function(){
-
-
if (moving) { //these four lines test to see if the start of "moving" if it is false or not
-
-
stopBtn._alpha = 0; // this guy sets the alpha(transparency to 0 to see the (start button after it moving has changed))
-
-
clearInterval(bounce); // this stops the interval therefore stopping the moving
-
-
moving = false; // this is required because this is the property that about which we know if the balls are moving or not
-
-
// it is also funny because we never actually test somehow to see if the balls change coordinates
-
-
// we just test to seee if "moving" is false or true and then we just start or stop the interval
-
-
//so this test is a work around. (this is a personal view )
-
-
}
-
-
else { //this is the else statement that set the motion back on
-
-
stopBtn._alpha = 100; // this restores the stopButton back to being opaque
-
-
bounce = setInterval(movement, 30);
-
-
moving = true;
-
-
}}
-
-
function movement(){ // This function moves all my bubles on the screen. (movement is the function set in the "setInterval" by 30 miliseconds)
-
-
for (count = 0; count{
-
-
balls[count]._x+= speedx [count];
-
-
balls[count]._y+= speedy [count];
-
-
if (balls[count]._x > 550-balls[count]._width/2 ) {
-
-
speedx[count] = speedx[count]*(-1);
-
-
}
-
-
if (balls[count]._y > 300-balls[count]._width/2 ) {
-
-
speedy[count] = speedy[count]*(-1);
-
-
}
-
-
if (balls[count]._x < (balls[count]._width/2) ) {
-
-
speedx[count] = speedx[count]*(-1);
-
-
}
-
-
if (balls[count]._y < balls[count]._width/2) {
-
-
speedy[count] = speedy[count]*(-1);
-
-
}
-
-
for (i = 0; iif (balls[i].hitTest(balls[count])) {
-
-
color = new Color(balls[count]);
-
-
color.setRGB(Math.random()*0xFF0000+0x00FF00+0x0000FF);
-
-
color = new Color(balls[i]);
-
-
color.setRGB(Math.random()*0xFF0000+0x00FF00+0x0000FF);}
-
-
trace(balls[count]._x);
-
-
trace(balls[count]._y);
-
-
}}
