Boolean values: True or False

In the movie below click the buttons to navigate through the movie.

Get Adobe Flash player

Instead of a separate function for each button, there is just one function. We use switch to determine which button was pressed.

The keyword switch is followed by the variable inside parentheses. The body of the switch block is enclosed in curly braces. When a matching value for the variable is found in the case statements, every executable statement is executed until there is a break.

The code is shown below:

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 goto
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);
}

Experiment: Download the movie, then make a new demo movie using the same code.

NEXT: OOPs: Object Oriented Programming