The best way to take advantage of Actionscript 3's awesome object oriented features is by working with external classes. By assigning a class to a movie clip in the Flash library, we can completely separate our code from our movie clips and graphics. Once this is done we are free to implement object oriented design patterns and take complete control of our application. So put away that "Actions Panel" and learn how to write Actionscript like a boss.

Consider the animation below, which was created with an external class:

The first step is to take control of the documents main time line by specifying our own Document Class. Since all objects are created within the main time line, we will be able to freely instantiate and control objects within it.

File - New - Actionscript file
File - Save - BallBounce.as

package {
    import flash.display.MovieClip;
    public class BallBounce extends MovieClip{
        public function BallBounce(){			

        }
    }
}

In this simple class we import the MovieClip class and extend it. We also define the constructor (always named after the class) which will be called when the main movie starts. The next step is to assign this class to the main movie time line.

Select File - Publish Settings
Under Flash Tab Select ActionScript Version: Version 3.0
Click Settings
Document Class = BallBounce

Now that we have taken control of the main time line, we can programmatically instantiate objects in it. In this example, we will create a Ball class as follows:

package{
     import flash.display.*;
     import flash.utils.Timer;
     import flash.events.TimerEvent;
     import flash.events.*;	

     public class Ball extends MovieClip{    

     private var radius;	
     private var moveTimer:Timer;
     private var speedx;
     private var speedy;

     public function Ball(size:int, 
       posx:int, posy:int, speed:int){	
			
     this.radius = size/2;
     this.x = posx; this.y = posy;
     this.width = size; this.height = size;
     this.speedx = speed; this.speedy = speed;

     this.moveTimer = new Timer(10,0);
     this.moveTimer.addEventListener(
     TimerEvent.TIMER, moveBall);	
     this.moveTimer.start();
     }

     public function moveBall(event:Event):void{
     this.x += speedx;
     if(this.x > (630-this.radius) || this.x < (0+this.radius)){
          speedx = -speedx;
     }
     this.y += speedy;
     if(this.y > (200-this.radius) || this.y < (0+this.radius)){
     speedy = -speedy;
     }
     }
     }
}

This class has 4 members, radius, which controls the balls size, speedx, speedy, which control the speed of the ball in either direction, and moveTimer, a Timer object that we will use to move the ball at certain intervals. These variables are assigned values in the Ball's constructor, and an event listener is assigned to the timer. Every 10 milliseconds, the moveBall function will be called. The 0 in the second parameter tells the timer interval to repeat indefinitely. If we only wanted the interval to repeat x number of times, a value greater than 0 could be used.

The move-ball function increments the balls x and y position by the values of speed x & y. It checks to see if the ball has left the boundaries of the stage, and if it has, reverses the speed value of the ball causing the ball to change direction.

Now to link the class to a movie object.

Right Click the Ball movie in the Library and select Properties
Set the class name, in this case we are using the Ball class we just created.
Note that the movie clip does not have to exist on the stage. We will be instantiating several of them from our document class next.

Now in our document class, we can instantiate any number of Balls we choose with different sizes, speeds and colors. The revised document class:

package {
import flash.display.MovieClip;
import flash.events.*;	

public class BallBounce extends MovieClip{			

     private var balls:Array = new Array();		

     public function BallBounce(){				

     for(var i=0; i<10; i++){
     var size = (Math.ceil(Math.random()*50))+10;
     var px = Math.ceil(Math.random()*(630-(size*2)))+size;
     var py = Math.ceil(Math.random()*(200-(size*2)))+size;
     var speed = (Math.ceil(Math.random()*3))+1;
     var ball = new Ball(size,px,py,speed);
     ball.gotoAndStop(i+1);
     balls.push(ball);
     addChild(ball);
     }	
}
}
}

In this example, we use a for loop to create 10 balls with random properties. The balls color is set by changing the ball movie's current frame with gotoAndStop. Finally, each ball is added to the main timeline with addChild.