OCEAN BUBBLE ANIMATION WITH ACTIONSCRIPT

 

CODE EXPLANATION

this.createEmptyMovieClip("holder", this.getNextHighestDepth());

This code creates and empty movieclip on stage

holder.maxbubbles=50;
var upspeed=80;

This part starts variables to be used in the script here maxbubbles gives the maximum no of clips to be attached and upspeed represents the speed of the bubbles

//Part 1 attaching the clips with loop
for(i=0;i<holder.maxbubbles;i++){
holder.attachMovie("circleclip","bubble"+i,i+5);
focusedbubble=holder["bubble"+i]
focusedbubble._x=random(Stage.width);
focusedbubble._y=random(Stage.height);
focusedbubble._yscale=focusedbubble._xscale=random(100);
focusedbubble._alpha=focusedbubble._yscale;
}

 

this is the loop to attach movieclip "circleclip" that we have exported for actionscript.Now clips named bubble1 bubble2 bubble3 etc will be attached to the empty movieclip "holder" according to the value of i.

In each loop the variable name "focusedbubble" is given to the i th movieclip and its properties like _xscale xposition etc are set.Now for alpha I have linked it with the scaling of clips so that big bubbles that are nearer will have high alpha and others away will have low alpha.Thus part 1 of the code is completed now we have to make movement to the clip.For that we should make an interval to repeat a function in a certain time, so that the y position and x position of each clip can be changed to create movement

//part 2 setting an interval for motion of bubbles
var bubbleup=setInterval(movebubble,80)

This code creates an interval to repeat the function so as to change the ypositon and xposition of the clips to create movement.

//function to repeat in the interval
function movebubble(){
for(i=0;i<holder.maxbubbles;i++){
focusedbubble=holder["bubble"+i]
focusedbubble._y-=upspeed/focusedbubble._alpha;
focusedbubble._x+=Math.sin(focusedbubble._y/10);
if(focusedbubble._y<=-20){
focusedbubble._y=Stage.height;
focusedbubble._x=random(Stage.width);
}
}
}

This is the function to repeat in the interval of .080 seconds as i have given in the code.Now when the code executes firstly the loop in on and for each value of i the value of xpos and y pos of the bubbles are modified after completing the loop again the function is repeated after .080 seconds and aging it enters the loop and so on. Here i have added a condition to check the y position of the loop i.e. if its less that -20 the position of that bubble is again set to initial value and random xposition at bottom.Here you can change the speed no of bubbles etc by modifying the variables

<<BACK

 
 
   
     

©2007 flashhelp.110mb.com