In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: bubbles

original code by Anthony Pace A.K.A. NME
Get Adobe Flash player
by NME 09 Jan 2015
    Embed
// forked from NME's bubbles
package {

    //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;
    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 sectionColours:Array;
        public var sectionEndX:Vector.<Number>;
        
        public var sectionWidth:Number;
        public var sections:Vector.<Vector.<uint>>;
        
        public var desiredFpsIntervalDuration:Number = 1000/60;
       
        public function FlashTest() {
            var i:int;
            
            sectionColours = [0xE52019, 0xFF6600, 0xFFA500, 0xD4FF00, 0x00FFA1, 0x2644D8, 0x00FFA1, 0xD4FF00, 0xFFA500, 0xFF6600, 0xE52019];
            sectionWidth = stage.stageWidth/sectionColours.length;
            sections = new Vector.<Vector.<uint>>(sectionColours.length,true);
           for (i = 0; i!= sections.length;i++){
                sections[i] = new Vector.<uint>(2,true);
                sections[i][0] = sectionColours[i];
                sections[i][1] = uint(sectionWidth*(i+1));
            }
            
            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['startTime'] = 0;//as the equation relies on time, this is going to be useful
            bubble['timePassed'] = 0;
            bubble['negRadius'] = 0;
            bubble['radius'] = 0;
            
            modBubble(bubble);
            return bubble;
        }
        public function modBubble(bubble:MovieClip):void{
            var g:Graphics = bubble.graphics;
            var colour:uint = 0xffaac66;
            //bubble.startTime = new Date().getTime();
            bubble.timePassed = 0;
            
            g.clear();
            bubble.x = Math.random()*stage.stageWidth;
            bubble.y = stage.stageHeight;
            
            for(var i:int = 0; i!= sections.length; i++){
                if(bubble.x<sections[i][1]){
                    colour = sections[i][0];
                    break;
                }
            }
            
            g.beginFill(colour,0.1+Math.random()*.2);
            bubble.radius = int(Math.random()*(15)+1);
            bubble.negRadius -=bubble.radius;
            
            
            
            bubble.Ay = -0.0000004*(bubble.radius*bubble.radius*bubble.radius);
            
            g.drawCircle(0,0,bubble.radius);
            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();
                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; //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<bubblePool[i].negRadius){ //less than 0 to appear as though the bubble is forming
                    modBubble(bubblePool[i]);
                    Dy = bubblePool[i].height*.25;
                    bubblePool[i].Ay*=0.5;//precomputing a later division by 2
                    //the time passed is changed to 0 when calling modBubble(BubblePool[i])
                    //so no need to do it here
                }else{
                    t = bubblePool[i].timePassed;
                    //since I modified Ay to be the precomputed value/2, I don't need to do it all the time
                    //thus I pulled it out
                    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;
            }
        }
    }
}