Refining the script

There is one major flaw in our script right now: we can't control the speed of the scrolling. For example, if the movie is set at 30 frames per second, then we scroll 30 lines a second! That's way too much. We have to find a way to skip a few frames.

A good way to achieve this is keeping a counter containing the number of the frame we're on. If that counter can be divided by a certain number, then you should scroll, otherwise not.

To check if a number is divisible by another one, we can use the modulo ( % ) operator. This gives us the remainder of a division. For example, 11 % 3 = 2, since 11 divided by 3 is 3, with 2 remaining. Knowing this, if frameNumber % skipFactor is equal to 0, then we should scroll, otherwise not.

However, when we first click on a button, we should be given immediate feedback regardless of what frame we're on. By setting the frameNumber to skipFactor when a button is clicked, the next time the onClipEvent( enterFrame ) code is fired, the text box will scroll immediately. This is not a simple caprice: fast response times are vital to user experience.

Knowing this, our final script becomes:

For containerMC:

onClipEvent (load){

 daTextBox = "Insert text here";
 scrolling = 0;
 frameCounter = 1;
 speedFactor = 3;

}

onClipEvent (enterFrame){

 if( frameCounter % speedFactor == 0){

  if( scrolling == "up" && daTextBox.scroll > 1){

   daTextBox.scroll--;

  }


  else if( scrolling == "down" && daTextBox.scroll < daTextBox.maxscroll){

   daTextBox.scroll++;

  }

  frameCounter = 0;
 }

 frameCounter++;
}

For the up button:

on( press ){

 scrolling = "up";
 frameCounter = speedFactor;

}
on( release, releaseOutside ){

 scrolling = 0;

}

For the down button:

on( press ){

 scrolling = "down";
 frameCounter = speedFactor;

}
on( release, releaseOutside ){

 scrolling = 0;

}

Note: You may need to adjust the speedFactor in the load clip event. Usually, 10 lines a second is a good speed, so speedFactor should be a tenth of the FPS of your movie. speedFactor needs to be an integer, though.

You now have a fully working and responsive scroller, just waiting to be used in your next project. I hope you enjoyed the tutorial and learned a few tricks here and there that you can use later. If you have any questions, or suggestions for another tutorial, please e-mail me at .

Be sure to be there for the next tutorial. We'll check out how to make a hierarchical menu.