Random Bubbles made with ActionScript 3.0

This is my first tutorial in witch I will explain how to make random bubbles with ActionScript 3.0. It’s a really nice effect.This is how our effect will look: Let’s start.

  • 1.Go to File – New – and make a new document
  • 2.On the new document rename the first layer to bg (from background) and make a new layer and name it actions. Lock the actions layer.
  • 3.Go to the tools palette select the rectangle tool and on the bg layer draw a rectangle that will cover the work area. Select the rectangle go to Windows-color .In the color palette select the fill ,set the gradient type to linear.For the first color type: #33CCFF and for the second #0033FF.

Ok now we have the background finished.

  • 4.Go to insert and select New Symbol. Set the name to bubbles and type to MovieClip and click ok.
  • 5.From the tools palette select the oval tool set the stroke to none and for the fill color pick a light blue, then draw a circle. Select the circle and in the Properties panel type 40 for the width and 40 for the height. With the circle selected go to Align panel and select align horizontal center and align vertical center to center the circle in the middle of the stage.
  • 6.Now let’s make our circle to look like a bubble. Select the circle go to Window-Color. In the color palette for the gradient select the radial type and type the colors:first color: #FFFFFF ; second: #00CCFF and third: #0099FF.
  • 7.Select the circle hit F8 to convert the circle to a MovieClip and set it’s name to bubble and click ok.
  • 8.In the timeline select frame 15 and hit F6 to insert a new keyframe. With frame 15 still selected ,on the stage select the bubble and in the properties panel type – 250 for y .On the same panel set the alpha to 0.
  • 9.Select first frame ,right clik and select Create motion tween.
  • 10.Go back to Scene1
  • 11.Go to Window- Library select the bubbles movieClip, right click ,select Linkage, select Export for ActionScript and in the class field type Bubbles.

Now let’s write some code.

  • 12.On scene1 select frame 1 on actions layer and hit F9 to open ActionScript panel.
  • 13.Select the code and paste in ActionScript panel.

var holder:Sprite = new Sprite();
addChild(holder);
holder.y = 400;
var arrayBubble:Array = [];
for (var i:int; i<200; i++) { arrayBubble[i]= new Bubbles(); holder.addChild(arrayBubble[i]); arrayBubble[i].x = Math.random() * stage.stageWidth; arrayBubble[i].scaleX = arrayBubble[i].scaleY = Math.random(); arrayBubble[i].gotoAndPlay(Math.ceil(Math.random()*15)); }

That is all. Hit Ctrl + enter to test your file(don't forget to remove the numbers before every line of code).It should work fine.

Let’s look at the code. We created the container(line 1) for our bubbles and added to the DisplayList(line 2). After we setup a for loop in witch we will loop 200 times (make 200 bubbles)in line 3. Setup an array that will hold our bubbles(line 4), position the holder that is the container for our arrayBubbles(line 5), populate the arrayBubbles with our bubbles(line 6) and add the arrayBubbles to our holder(line 7). In line 8 we position the bubbles on the x axis.The bubbles will have a random x between 0 and stageWidth. In line 9 we scale our bubbles And finally in line 10 we instruct the bubble to go an play one frame from our 15 frames animation. That is all.

If you have questions, suggestions, doubts about the tutorial don’t hesitate to comment.I will see you on next tutorial. You can download the source file from here: Random bubbles.

Comments

Anonymous said…
Thanks for the tutorial.

I think there's a typo in line #6. Bubbles is spelled Bubbels.
Sorin said…
Yes you have right it was a typo, I fixed the misspelling.Thanks for notify me.
Anonymous said…
thanks for the tutorial. i followed your steps and got an error at line 11 - arrayBubble[i] = new Bubble(); apparently it's calling an undefined method. any ideas where i've went wrong? thanks!
Sorin said…
Hello.
I have eating an "s". Change your line from:
arrayBubble[i] = new Bubble() to arrayBubble[i] = new Bubbles().That should fix the problem. Please tell me if it works.
Anonymous said…
Hi man, nice tutorial
everything is working fine...
but instead of having a random effect with bubbles continously generated, I'm just creating waves of 200 bubbles at time.
Anonymous said…
thank you!!! that fixed it right up... you gotta love a typo... excellent tutorial... a great way to get my feet wet...
Anonymous said…
I followed the tutorial but I got an error message on lines 1 and 3.

1. The class or interface 'Sprite' could not be loaded.
Sorin said…
The script it’s write in ActionScript 3(Flash CS3). Are you using ActionScript 3?
terrill13 said…
Brilliant - thanks so much! Minimum code with maximum bang - thank you for posting this! Worked perfect the first time!
Anonymous said…
var holder:Sprite = new Sprite();

addChild(holder);

for (var i:int; i<50; i++) {

var arrayBubble:Array = new Array();

holder.y = 400;

arrayBubble[i] = new Bubbles();

holder.addChild(arrayBubble[i]);

arrayBubble[i].x = Math.random()*stage.stageWidth;

arrayBubble[i].scaleX = arrayBubble[i].scaleY = Math.random();

arrayBubble[i].gotoAndPlay(Math.ceil(Math.random()*15));

}




I keep getting a call to an undefined method on line 11. any ideas?
Fale said…
Hi, thanks for the tutorial, worked perfectly for me. However I have some elements on the stage and the bubbles should run behind those elements. How can I fix that?
Thanks a lot, very much
Alejandro
Sorin said…
Post the error that the Flash IDE throws and not the code.
Download the source file and take a look there and make sure that you haven’t skipped the 11 step of the tutorial.
Sorin said…
Hi Alejandro,
Make a new layer and drag it above the bg layer and put your elements on the layer you just created.
Fale said…
Hi again. Just wanted to share this with you because is very weird. All the bubbles are now suddenly aligned to the left of the canvas when previewing the html where the swf is embeded. When played alone the swf looks fine. However, I created a new html page and also looks fine, so not sure why is doing this in the original html where worked just fine until today. Important: is happenning only in Firefox, Safari and Opera are ok. And I recently downloaded a new version of Firefox, so who nows. Here are the links:

NOT OK
http://www.adoblevestudio.com.ar/basejobs/cocacolafemsa/index.html

OK
http://www.adoblevestudio.com.ar/basejobs/cocacolafemsa/test.html

Thanks again,
Alejandro
Sorin said…
Hi Alejandro
I have checked your links but I don’t see any problem, the bubbles are fine. My Firefox version is 3.03.
I see that you are not using SwfObject to embed the sfw in the html page. You should use it its the most reliable method to embed swfs into html. You can check it here: swfobject
Fale said…
Thanks a lot man. Have a nice day.
Anonymous said…
Very nice. Works great!
Junry said…
Guys, I have a favor what if I have to put the bubbles on a certain movie clip? how would I do it? newbie me...
Sorin said…
Hi Heero,
They are on a movie clip (a sprite actually) the holder sprite. You can replace the holder sprite with your movie clip and add the bubbles to it.
Junry said…
Halu guys, this Random Bubbles is made with AS 3.0, I forgot that I use Actionscript 2.0 and I run into problems with Sprite "class or interface Sprite could be loaded".
Is there any work-around for this to work on Actionscript 2.0?

Thanks for the reply.
Sorin said…
Hi Heero,
Sure, this can be done in as2.If you are a beginner don’t waste time learning as2.Learning as2 and after switching to as3 it won’t help you, it will confuse you.
Junry said…
Halu guys, I need an animation like this to work on ActionScript 2.0. I know that it is good to use ActionScript 3 but I have to work around to this to make it work on ActionScript 2.0.
Sorin said…
Sorry Heero, I don’t use as2 anymore. Try a search on Google, you will find for sure something similar.

Popular posts from this blog

Photo gallery (AS 3.0).

ActionScript 3 button that stay in the down state.

Custom Scrollbar