The following code will allow you to create a Bubble Effect in Flash CS4/AS3. To get the script to work correctly you need to have a movieClip in your library with the class name “bubble_class”
This AS3 class will create the bubble effect.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
class bubbles {
private var target:Object;
//private var imageClass:Object;
private var speed:Number=.75;
private var current:Number=.25;
private var xpos:Number=1000;
private var xoffset:int=0;
private var ypos:Number=800;
private var yoffset:int=0;
private var amount:int;
private var left:int;
private var right:int;
private var top:int;
private var bottom:int;
public function bubbles(target:Object,amount:int = 100):void {
this.target=target;
this.amount=amount;
}
public function setBounds(x1:int,x2:int,y1:int,y2:int):void {
this.left=x1;
this.right=x2;
this.top=y1;
this.bottom=y2;
this.xpos=this.calculateBoundLength(x1,x2);
if (x1<0) {
this.xoffset=x1*-1;
}
this.ypos=this.calculateBoundLength(y1,y2);
if (y2<0) {
this.yoffset=y1*-1;
}
}
private function randomPos(length:int, offset:int):int {
return (Math.random()*length)-offset;
}
private function calculateBoundLength(start:int, end:int):int {
if (start<0) {
start=start*-1;
}
return start+end;
}
public function init():void {
//run a for loop based on numberOfBubbles
for (var i = 0; i < this.amount; i++) {
//set temporary variable and attach snowflake to it from the library
var bubble:bubble_class = new bubble_class();
target.addChild(bubble);
bubble.x=this.randomPos(this.xpos,this.xoffset);
bubble.y=this.randomPos(this.ypos,this.yoffset);
bubble.rad=Math.random()*this.current-Math.random()*this.current;
bubble.addEventListener(Event.ENTER_FRAME,this.bubbleEnterFrameHandler);
}
}
public function bubbleEnterFrameHandler(event:Event) {
var b:Object=event.currentTarget;
//update bubble position
b.x-=b.rad;
b.y-=speed;
//if bubble out of bounds, move to other side of screen
if (b.y<=this.top) {
b.y=this.bottom;
}
if (b.x>=this.right) {
b.x=this.left;
}
if (b.x<=this.left) {
b.x=this.right;
}
}
}
}




March 23, 2010 at 9:51 am
Nice tut…
February 7, 2011 at 1:03 pm
its showing an error for the package line