OOPs: More Classes

Next, we will copy all of the functions from the bubble movie to the class. We will replace the word "ball" with "this"

The code that is executed once is any code that is not inside a function:

package  {
	//imports
	public class Bubble extends MovieClip {
       //variables
       var dx:int=0;
       public var dy:int=-2;
       var origX:int;
       
	   public function Bubble() {
			// constructor code: anything that happens ONCE at the beginning
			addEventListener(Event.ENTER_FRAME,frames);
            origX=this.x;
		} //constructor
      //other functions
      function frames(event:Event):void {
	     dx=Math.random()*5; //0 to 4
	     dx-=2; //-2 to +2
	     this.x+=dx;
	     this.y+=dy;
	     if(this.y<0-this.height) {
		    this.y=stage.stageHeight+this.height;
		    this.x=origX;
	     }
       } //frames
	} //class
} //package

It is a good idea to save after each step because you are moving back and forth between the as file and the code in the original movie.

NEXT: Imports