// forked from NME's Stream of bubbles
//Stream of bubbles
// forked from NME's bubbles
package {
import flash.events.MouseEvent;
//original code by Anthony Pace A.K.A. NME
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.utils.setInterval;
import flash.text.TextField;
[SWF(width=465, height=465, backgroundColor=0xffffff, frameRate=121)]
public class FlashTest extends MovieClip{
public var maxBubbles:uint = 500;
public var bubblePool:Vector.<MovieClip> = new Vector.<MovieClip>;
public var iid:Number;
public var pauseTimeStart:Number;
public var maxHeight:uint;
public var colours:Vector.<uint>;
public var desiredFpsIntervalDuration:Number = 1000/60;
public var timePassed:uint;
public var deg2rad:Number = Math.PI/180;
// public var rad2deg:Number = 180/Math.PI;
public var degToRadX2:Number = deg2rad*2;
public var startXOffset:Number;
public var mouseIsDown:Boolean=false;
//this is a TextField I used for debugging
//uncomment and use dbgEcho('your message here')
/* public var dbgTxt:TextField;
public function dbgEcho(s:String):void{
var tfw:Number,tfh:Number;
dbgTxt.appendText('\n'+s);
tfw = dbgTxt.textWidth+4;
tfh = dbgTxt.textHeight+4;
if(tfw>stage.stageWidth){
tfw=stage.stageWidth;
}
if(tfh>stage.stageHeight){
tfh=stage.stageHeight;
}
dbgTxt.width = tfw;
dbgTxt.height = tfh;
dbgTxt.scrollV = dbgTxt.maxScrollV;
}
public function dbgClr():void{
dbgTxt.text = '';
}*/
public function mMoveEH(e:MouseEvent):void{
if(e.buttonDown){
mouseIsDown=true;
startXOffset = e.stageX;
// dbgEcho(String(this.startXOffset));
}else{
mouseIsDown = false;
}
//dbgEcho(String(this.startXOffset));
e.updateAfterEvent();
}
public function FlashTest() {
var i:int;
/*dbgTxt = new TextField();
stage.addChild(dbgTxt);
dbgEcho('\n\n\n dbgTxt appears to be working');
*/
maxHeight = 0;
colours = new <uint>[0xE52019, 0xFF6600, 0xFFA500, 0xD4FF00, 0x00FFA1, 0x2644D8, 0x00FFA1, 0xD4FF00, 0xFFA500, 0xFF6600, 0xE52019];
colours.fixed=true;
this.startXOffset = uint(stage.stageWidth/2);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mMoveEH);
this.iid = setInterval(moveBubbles,desiredFpsIntervalDuration);
}
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['timePassed'] = 0;
bubble['negRadius'] = 0;
bubble['radius'] = 0;
bubble['startXOffset'] = this.startXOffset;
modBubble(bubble);
return bubble;
}
public function modBubble(bubble:MovieClip):void{
var g:Graphics = bubble.graphics;
var colour:uint = 0xffaac66;
bubble.timePassed = 0;
g.clear();
bubble.x = this.startXOffset;
bubble.y = stage.stageHeight;
colour=colours[int(Math.random()*colours.length)];
g.beginFill(colour,0.1+Math.random()*.2);
bubble.radius = 1+Math.random()*5;
bubble.negRadius -=bubble.radius;
bubble.Ay = -0.0000004*( bubble.radius);
g.drawCircle(0,0,bubble.radius);
//becase there is a lot of alpha blending going on, I am not sure this will really help
//in truth manually rasterizing your images is better, but I won't concern myself with
//this right now
//bubble.cacheAsBitmap = true;
bubble.z = bubble.radius*Math.sin((-1+Math.random()*2));
}
public function newBubbles(newBubbleCount:uint=0):void{
var i:uint = 0,randMax:uint = 8,count:uint,ln1:uint;
if(maxBubbles-bubblePool.length <randMax){
randMax =int(maxBubbles-bubblePool.length);
}
count = newBubbleCount;//Afterwards I am going to have to cut down on variable usage like this
//and clean up the code in general, but it works
if (newBubbleCount==0){
count = 1+Math.random()*randMax;
}
while(i!=count){
ln1 = bubblePool.length; //after create, it will be equal to length-1
bubblePool[bubblePool.length] = createBubble();
addChild(bubblePool[ln1]);
++i;
}
}
public function moveBubbles():void{
var lbp:uint,i:uint = 0,Ay:Number,t:Number,Dy:Number;
this.timePassed= (this.timePassed+1)%360;
var cos_tpXd2r:Number=Math.cos(this.timePassed*this.deg2rad);
this.startXOffset += cos_tpXd2r;//
//dbgEcho(this.timePassed.toString()+" "+this.startXOffset.toString()+' '+Math.cos(this.timePassed*this.deg2rad));
//this.startXOffset -= stage.stageWidth*Math.cos(this.timePassed*this.deg2rad); //alternative looks cool, so I kept it here
if(bubblePool.length!=maxBubbles){
newBubbles(1);
}//
lbp = bubblePool.length; //length of the bubble pool
while(i!=lbp){
bubblePool[i].timePassed+=desiredFpsIntervalDuration;//16.66666666... is 1000/60... the milliseconds desired for the frame rate;
if (bubblePool[i].y<maxHeight){ //less than 0 to appear as though the bubble is forming
modBubble(bubblePool[i]);
//I will have to strip this later for things I can pre-compute
bubblePool[i].startXOffset = this.startXOffset;
bubblePool[i].y= stage.stageHeight;
bubblePool[i].z= 100*Math.sin((1+Math.random()*360)*this.deg2rad);
}else{
t = bubblePool[i].timePassed;
// this is a basic equation for finding the delta based on time
//I removed initial velocity x time, or V0y*t because I don't need an initial velocity in this case
Dy =-0.17*t+/* Voy*t +*/
(bubblePool[i].Ay * t * t)*.5;
//lets make a torn
var t2:Number = t*2;
var trad:Number = t*deg2rad;
var sint:Number = Math.sin(trad);
//amplitude should get bigger as time moves on, so I put this here as Amplitude plus a time dependent inverted dampening factor
var A:Number=t/(this.desiredFpsIntervalDuration);
bubblePool[i].y = stage.stageHeight + Dy;
bubblePool[i].x =bubblePool[i].startXOffset - A*Math.cos(bubblePool[i].y*this.degToRadX2);
//
//I should so something with the z-axis, but it's looking good to me as it is
//bubblePool[i].z += bubblePool[i].startXOffset;//makes it look like a river
//bubblePool[i].z += cos_tpXd2r;//more 3d
//bubblePool[i].z+=bubblePool[i].x*0.01;
}
++i;
}
}
}
}