//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;
[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;
public function FlashTest() {
init();
}
public function init():void{
var i:int;
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 mMoveEH(e:MouseEvent):void{
if(e.buttonDown){
mouseIsDown=true;
startXOffset = e.stageX;
}else{
mouseIsDown = false;
}
e.updateAfterEvent();
}
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 +Math.random()*20-20;
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=1):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+Math.random()*10-5;//
var bpi:MovieClip;
if(bubblePool.length!=maxBubbles){
newBubbles();
}//
lbp = bubblePool.length; //length of the bubble pool
while(i!=lbp){
bpi=bubblePool[i];
bpi.timePassed+=desiredFpsIntervalDuration;//16.66666666... is 1000/60... the milliseconds desired for the frame rate;
if (bpi.y<maxHeight){ //less than 0 to appear as though the bubble is forming
modBubble(bpi);
//I will have to strip this later for things I can pre-compute
bpi.startXOffset = this.startXOffset;
bpi.x = this.startXOffset+Math.random()*20;
bpi.y= stage.stageHeight;
bpi.z= 100*Math.sin((1+Math.random()*360)*this.deg2rad);
}else{
t = bpi.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.3*t+/* Voy*t +*/ (bpi.Ay * t * t)*.5;
//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=0.3*(t/(this.desiredFpsIntervalDuration));////
bpi.y = stage.stageHeight + Dy;
bpi.x -=A*Math.cos(bpi.y*this.degToRadX2);
bpi.z += cos_tpXd2r;//more 3d
}
++i;
}
}
}
}