How to create bubbles in flash (ActionScript 2.0)
Here is a simple peice of ActionScript that can also be used to create snow or rain. I've seen a few particle engines on the net that I easily could have used but I wanted something simpler.
So this is what I came up with.
var total:Number = 30; //total number of bubble var sw:Number = Stage.width; //stage width var sh:Number = Stage.height; //stage height for (var i=0;i<total;i++){ var bubble:MovieClip = this.attachMovie("mcBubble", "mcBubble"+i,this.getNextHighestDepth()); // set the initial random speed bubble.yspeed = random(500) + 3; // set the initial random position bubble._y = sh; bubble._x = random(sw); // set the initial wobble factor (the X movement) bubble.xspeed = random(3); if (random(2) eq 1) { bubble.xspeed = -bubble.xspeed; } bubble.onEnterFrame = function(){ // the bubble movement this._y = this._y - this.yspeed; // check if the bubble is out of the top of the screen if (this._y <= -15) { // then reset to the bottom this._y = sh; this._x = random(sw); this.yspeed = random(5) + 3; // now to randomise the scale of the bubble so // we get a few wee ones too scale = 2 + (random(8)); this._width = scale; this._height = scale; // set the initial wobble factor (the X movement) this.xspeed = random(3); if (random(2) eq 1) { this.xspeed = -this.xspeed; } } // change the X value of the bubble this._x = this._x + this.xspeed; // the bounce code for either the left or right of the // margin for the bubble if (this._x > 100 or this._x < 50) { this.xspeed = -this.xspeed; } } }
Easy.. now this is what it looks like.
You could make it more realistic by tweaking the speed, wobble values and add blurs or better graphics but what I needed it for, this is perfect.
Download the Source Code - Use it any way you want, just let others know how you used it by leaving a comment. Peace!
Enjoy.

nice AS2 code…
thanks
Really good work about this website was done. Keep trying more – thanks!
How do i stop this in 10 frames time? So it only goes for the ten frams that I want?
Do you mean a timeframe? For example the bubbles stop witin 10 sec?
hi,
thanks great work!
how can i make the bubbles bigger?
cheers
Hi Mazza,
You can either resize the actual bubble asset from the library or modify the following in the code and add a buffer.
Original code:
—————————————-
scale = 2 + (random(8));
this._width = scale;
this._height = scale;
—————————————-
Edited code:
—————————————-
var buffer:Number = 50;
scale = 2 + (random(8));
this._width = scale+buffer;
this._height = scale+buffer;
—————————————-
Hope that helps….
Hi i need to make a fish tank project so im using your code to make the bubbles. But i only want them in two scenes (i need to make an into, middle and outro, and i dont want them in outro). My question is how can i get the bubbles to stop at the end of the second scene? Thank you in advance!!!