The document class is like any other class except that instead of being for an object inside a movie it is the code for the movie itself.
Consider the "How to make tea" movie below. The buttons let us move from one frame to the next, go back to the beginning. It is conceivable that we might make dozens of movies with the same code. We will create a class called Navigation and use it as the base class for the movie instead of copying the code into each one.
stop(); //to keep the movie from looping through all the frames
this.btnFirst.addEventListener(MouseEvent.CLICK,goFrame);
this.btnBack.addEventListener(MouseEvent.CLICK,goFrame);
this.btnNext.addEventListener(MouseEvent.CLICK,goFrame);
this.btnLast.addEventListener(MouseEvent.CLICK,goFrame);
function goFrame(e:MouseEvent):void {
var frm:int; //which frame to go to
switch(e.target) {
case btnFirst: frm=1; break;
case btnNext: frm=this.currentFrame+1; break;
case btnBack: frm=this.currentFrame-1; break;
case btnLast: frm=this.totalFrames; break;
}
if(frm<1) frm=1;
if(frm>this.totalFrames) frm=this.totalFrames;
btnFirst.enabled=frm>1; //btnFirst is enabled if we are not already in the first frame
btnBack.enabled=frm>1; //can't go back if we are in the first frame
btnNext.enabled=frm<this.totalFrames; //can't go forward if we are in the last frame
btnLast.enabled=frm<this.totalFrames; //no point in going to last frame if we are already in last frame
gotoAndStop(frm);
}
The code requires that there be buttons called btnFirst, btnBack, btnNext, and btnLast.
Experiment: Download the movie
INDEX, The Document Class, Create a Navigation Class, Create New Movie with the Navigation Class, A Puzzle Document Class, Puzzle Demo, The Bear Puzzle
Next lesson: Communication