Simple Scrollbar Class

Here’s a simple AS 2.0 Class that creates a scrollbar. The ScrollBar class takes two parameters. The name of the movie clip that will act as the scrollbar and the name of a function the scrollbar will call when it is dragged.

new ScrollBar( target_mc, callBack )

The target_mc must have two clips inside named drag_mc and track_mc. The first is the dragger and the second sets the limits for the dragger. You can design these in any way you like as long as they use the instance names

  • drag_mc
  • track_mc

For best results place the registration points of both clips in the upper left corner and align both clips within their parent.

The callback can be any function name that you have defined. The ScrollBar class will send a value from 0 to 1 to the callback that gives the percentage position of drag_mc from the top of track_mc.

For example if you made a clip with both drag_mc and track_mc you might give that clip the instance name of scroll_mc. To make a new instance of ScrollBar you could use the following:

var a_scrollbar:Scrollbar = new Scrollbar( scroll_mc, test );

When the scrollbar is dragged the function test would be called and passed a value between 0 and 1 representing the position of the dragger. You could use this to scroll some text in a dynamic text field or set the volume of a sound object.

Here is a sample function that would scroll a dynamic text field named scroll_txt:

function test( n ) {
scroll_txt.scroll = Math.round( ( scroll_txt.maxscroll - 1 ) * n ) + 1;
}

Simple Scrollbar Class

Here’s an example of the scrollbar in action.

[kml_flashembed movie=”examples/Scrollbar/Scrollbar.swf” height=”156″ width=”314″ /]

Scrollbar( host_mc:MovieClip, call_back_func:Function )

Creates a new Instance of Scrollbar. The host_mc clip must contain two clips named drag_mc and track_mc. These clips act as the scrollbar. track_mc sets the limits while drag_mc is the interactive clip.

The call back function can be the name of any function you define. It will receive one parameter, a value representing how far the scrollbar is scrolled. The range of the returned value will be from 0.0 to 1.0. With 0.0 being the value returned when the scrollbar is at the top (or left when scrolling horizontally) and 1.0 when the scrollbar is at the bottom (or right). You can use this value to control elements in your movie.

  • host_mc: a movie clip containing the drag_mc and track_mc.
  • call_back: the name of a function that will be called when the scrollbar is scrolled.

scroll_horizontal( horizontal_scroll:Boolean )

This method determines whether the scrollbar scrolls vertical or horizontal. By default the scrollbar scrolls vertically. Calling scroll_horizontal() and passing true makes the scrollbar scroll horizontally.

getValue():Number

The method returns the current value of the scrollbar.

setValue( n:Number )

The method sets the current position of the scrollbar. Pass a value of 0 to 100.

Property enabled

This property enables and disables the scrollbar.

3 thoughts on “Simple Scrollbar Class”

  1. Hi Mitchell,
    I have a question about scrolling info in the site. I was able to put the scrollbar to scroll text in my project. However, I have a problem to incorporate images so that the scrollbar can scroll everthing together.

    Also, I have a question about dynamic text. With dynamic text, I am not able to change part of the text from the text box to other font, size and color. Is there any way to have two text boxes and one scrollbar?? I tried different ways as much as I could, and I still could not make it work.

    Hope you can offer me some help. Thank you very much.

    Cammie

  2. This sounds like a question that should be posted to the forum. The easiest way would be to use HTML text in your text field and use the img tag to place images in the field.

Leave a Reply to admin Cancel reply

Your email address will not be published. Required fields are marked *