// forked from nme's bubbles
package {
//import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.utils.setInterval;
public class FlashTest extends MovieClip{
public var maxBubbles:uint = 300,bubblePool:Vector.<MovieClip> = new Vector.<MovieClip>;
public function FlashTest() {
var iid:Number = setInterval(moveBubbles,1000/60);
}
public function createBubble():MovieClip{
var bubble:MovieClip = new MovieClip();
bubble['Ay'] = 0; //The will be the acceleration constant along Y... for the the lift
bubble['startTime'] = 0;//as the equation relies on time, this is going to be usefl
modBubble(bubble);
return bubble;
}
public function modBubble(bubble:MovieClip):void{
var g:Graphics = bubble.graphics;
bubble.startTime = new Date().getTime();
bubble.Ay = -0.001+Math.random()*((9.8-13.4)/1000);
g.clear();
//g.lineStyle(2,0xff0000+int(Math.random()*0x00ff00),1,true);
g.beginFill(0xff0000+int(Math.random()*0x0000ff),Math.random()*.5);
g.drawCircle(0,0,int(Math.random()*25)+5);
bubble.cacheAsBitmap = true;
}
public function newBubbles():void{
var i:uint = 0,randMax:uint = 8,count:uint,ln1:uint;
if(maxBubbles-bubblePool.length <randMax){
randMax =int(maxBubbles-bubblePool.length);
}
count = 1+Math.random()*randMax;
while(i!=count){
ln1 = bubblePool.length; //after create, it will be equal to length-1
bubblePool[bubblePool.length] = createBubble();
bubblePool[ln1].x = Math.random()*stage.stageWidth;
bubblePool[ln1].y = stage.stageHeight;
addChild(bubblePool[ln1]);
++i;
}
}
public function moveBubbles():void{
var lbp:uint,i:uint = 0,Ay:Number,t:Number,Dy:Number;
if(bubblePool.length!=maxBubbles){
newBubbles();
}
lbp = bubblePool.length;
while(i!=lbp){
if (bubblePool[i].y+bubblePool[i].height<0){
Dy = 0;
modBubble(bubblePool[i]);
}else{
t = new Date().getTime() - bubblePool[i].startTime;
Dy =/* Voy*t +*/ (bubblePool[i].Ay * t * t)*0.5; //I can get rid of Voy*t as it Voy will always be 0 in this case;
}
bubblePool[i].y = stage.stageHeight + Dy;
++i;
}
}
}
}