OOPs: More Classes

Get Adobe Flash player

In this movie we have a bubble that starts at the bottom and moves up. When it leaves at the top, it starts again at the bottom in the same x location that it started.

var dx:int=0;
var dy:int=-2;
var origX:int;
addEventListener(Event.ENTER_FRAME,frames);
origX=ball.x;
function frames(event:Event):void {
	dx=Math.random()*5; //0 to 4
	dx-=2; //-2 to +2
	ball.x+=dx;
	ball.y+=dy;
	if(ball.y<0-ball.height) {
		ball.y=stage.stageHeight+ball.height;
		ball.x=origX;
	}
}

Download the finished movie

We would like to have more than 1 bubble in our movie, or we think this is code that might be useful in more than 1 movie. Therefore, we would like to create a Bubble class.

NEXT: Create a class: Bubble