Custom Scrollbar
In today tutorial we will make a custom scrollbar. For this tutorial we will use ActionScript 3.0
There are a lot of tutorials on the net with this subject but more of them are using a static text field (they draw a text field on the stage and type the text in that field).This approach does not offer a lot of flexibility. In our tutorial we will use a dynamic text field (the text is not typed on the stage it comes from an external source).
Ok. Now let’s go back to work.
Your settings must like like this:
Ok now we finished the graphical part. Let’s write the code .
Should work fine.
Next we will look at the code.
In line 1 we declared a variable that hold’s the text that will be displayed by our Text Fiels.
In line 2 we assign that variable to our text field and set the wordWrap property to true.
After we declared a variable for our minScroll this hold the minimum positions for our scrollHandler_mc.
The maxScroll variable holds the maximum positions of scrollHandler_mc.
scrollInterval variable holds the interval in witch our scrollHandler_mc can move.
In lines 9 and 10 we add two eventListeners ,these will keep track of Mouse_DOWN and Mouse_UP events.
Function beginDrag() handle’s the Mouse_DOWN event. In this function we enabled our scrollHandler_mc, you can drag it and set the boundaries for it.We register another listener(Event.ENTER_FRAME) that will update the scroll property of our myTfield base on how much we drag the scrollHandler_mc.This behavior is handle by the function checkingProgress().
The function endDrag() disable the drag property of scrollHandler_mc.
I hope that I was clear enough.
If you have questions, suggestions, doubts about the tutorial don’t hesitate to comment.I will see you on next tutorial.
You can download the source file from here:Custom Scrollbar.
There are a lot of tutorials on the net with this subject but more of them are using a static text field (they draw a text field on the stage and type the text in that field).This approach does not offer a lot of flexibility. In our tutorial we will use a dynamic text field (the text is not typed on the stage it comes from an external source).
Ok. Now let’s go back to work.
- 1.Go to File – New select Flash file(ActionScript 3.0) and click Ok.
- 2.Double click on the layer1 name and rename it text.
- 3.Select the Text tool in the Properties panel set the Text type to Dynamic text, the Line type to Multiline and set the color to black.On the stage click and drag to make a text field. In the Properties panel set the width to 200 ,height to 150, x to 175 and y to 125.Set the instance name myTfield.
Your settings must like like this:
- 4.Insert new layer ( Insert – Timeline – Layer ) double click on the layer 2 name and rename it to track.
- 5.Lock the text layer. Select the Rectangle tool and on the track layer draw a rectangle. Set is width to 15 ,height to 150, X to 376 and Y to 125 and pick a color. I pick a orange one (#FF9900).
- 6.Select the rectangle hit F8 name it track_mc, set it’s type to Movie Clip and set it’s registration point in to upper left like in the picture.Set the instance name to track_mc.
- 7. Insert new layer ( Insert – Timeline – Layer ) double click on the layer 3 name and rename it scrollHandler.
- 8. Lock the track layer. Select the Rectangle tool and on the scrollHandler layer draw a rectangle. Set is width to 15 ,height to 15, X to 376 and Y to 125 and pick a color. I pick black.
- 9. Select the rectangle hit F8 name it scrollHandler_mc, set it’s type to Movie Clip and set it’s registration point in to upper left like before.Set the instance name to scrollHandler_mc.
Ok now we finished the graphical part. Let’s write the code .
- 10. Insert new layer ( Insert – Timeline – Layer ) double click on the layer 4 name and rename it actions.
- 11.Select frame 1 in the actions layer hit F9 to open the ActionScript panel.
- 12.Copy and paste the code in the ActionScript panel.
1. var myMessage:String = "If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy! If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy! If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy! If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy! If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy! If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy! If you like the scroll bar and want to know how I made it I have posted a detailed tutorial on my blog: http://biochimistu.blogspot.com.Thanks and enjoy!";
2. myTfield.text = myMessage;
3. myTfield.wordWrap = true;
4. var minScroll:Number = scrollHandler_mc.y;
5. var maxScroll:Number = minScroll+(track_mc.height -scrollHandler_mc.height);
6. var scrollInterval:Number = maxScroll - minScroll;
7. var draging:Boolean = false;
8. var bounds:Rectangle = new Rectangle(scrollHandler_mc.x,scrollHandler_mc.y,0,track_mc.height - scrollHandler_mc.height);
9. scrollHandler_mc.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
10. stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
11. function beginDrag(event:MouseEvent):void
{
scrollHandler_mc.startDrag(false,bounds);
draging = true;
scrollHandler_mc.addEventListener(Event.ENTER_FRAME,checkingProgress);
}
12. function endDrag(event:MouseEvent):void
{
scrollHandler_mc.stopDrag();
draging = false;
}
13. function checkingProgress(event:Event):void
{
var moveDrag:Number = scrollHandler_mc.y - minScroll;
var procentDrag = moveDrag / scrollInterval;
if (draging)
{
myTfield.scrollV = procentDrag * myTfield.maxScrollV;
}
}
Remove the numbers before every line of code. Hit Ctrl + Enter to test your movie.Should work fine.
Next we will look at the code.
In line 1 we declared a variable that hold’s the text that will be displayed by our Text Fiels.
In line 2 we assign that variable to our text field and set the wordWrap property to true.
After we declared a variable for our minScroll this hold the minimum positions for our scrollHandler_mc.
The maxScroll variable holds the maximum positions of scrollHandler_mc.
scrollInterval variable holds the interval in witch our scrollHandler_mc can move.
In lines 9 and 10 we add two eventListeners ,these will keep track of Mouse_DOWN and Mouse_UP events.
Function beginDrag() handle’s the Mouse_DOWN event. In this function we enabled our scrollHandler_mc, you can drag it and set the boundaries for it.We register another listener(Event.ENTER_FRAME) that will update the scroll property of our myTfield base on how much we drag the scrollHandler_mc.This behavior is handle by the function checkingProgress().
The function endDrag() disable the drag property of scrollHandler_mc.
I hope that I was clear enough.
If you have questions, suggestions, doubts about the tutorial don’t hesitate to comment.I will see you on next tutorial.
You can download the source file from here:Custom Scrollbar.
Comments
I will post here one tutorial that shows how to make this type of Scrollbar.If I have enough time I will write it this week if no the next week.
Have fun flashing.
just gotta say the guide is awesome, well done.
cheers,
mike.
I don’t know if I understand your question. The TextField by default has no background, try matching your swf background with the background of the application where you want to load the sfw. Yes it is possible to format the text, read this tutorial Format text with CSS you will find some helpful things in there.
Try reading from the ActionScript Official Documentation about TextField class.I hope that this helps you.
Ok, now I understand, you want to make your sfw background
transparent. Go to
How to make a Flash movie with a transparent background you will find a very good explication on how to do that.
Basically you will need to modify the Publish Settings of your swf or to make a little modification to the HTML code.
This should solve your problem.
I love your custom scroll script - thank you, but how would I change your script to make the text load from an external text file? Thank you so much, Shawn.
Read the How to load, display and scroll external text. tutorial, you will find there the code needed to load external text.
I know how to do scrolling text with up/down buttons, I was just hoping that you knew how to do it using the custom scrollbar. If you do know, please, share! Thank you for your help, Shawn
You have not understood me or maybe I don’t make my self clear. You said that you want to make "the text load from an external text file". I told you to go to that tutorial to see how to load an external txt file and to add the text to the Text Field. But anyway make this modification to the script:
-remove the first three lines of code
-place the next code:
//--------------loading external text and display it---------
var textRequest:URLRequest = new URLRequest("sample.txt");
var textLoader:URLLoader = new URLLoader();
textLoader.load(textRequest);
textLoader.addEventListener(Event.COMPLETE,
fileLoaded);
function fileLoaded(event:Event):void {
myTfield.text = event.target.data;
myTfield.wordWrap = true;
}
Now the text comes from an external file and you can use the custom scrollbar.
I used this scrollbar code and it works well, and I use it with dynamic text. Thank you.
However, I have faced a problem: when a dragger (scrollHandler_mc) was moved to a position lower than its initial one, dwitching to another scrollable text area in another frame will have the dragger in the same position and it will be draggable below the limits of track_mc. In order to restore it, I need to refresh the website page. You can look at it: http://www.survivorsresource.narod.ru/flash/flash.html
Some pages of my site have text which is scrollable, so when you scroll this page and don't return the scrollHandler in the initial position, yu will have problems scrolling text in other sections of the website.
How the code can be changed?
Thanks.
The value of the variable will be equal with initial y position of the scrollHandler_mc.
If have not tested this (I don’t have time) but I think that should work.
No, you can’t use this scrollbar in an as 2 file.The scrollbar script is written in AS 3.0 and will not work in AS 2.0.You can try to adapt the script to as 2 or the easy way search a scrollbar that is written in as 2.
What do I have to change?
Is that "MovieClip" supposed to be just "MovieClip" or the instance name of the movie clip the textField and scrolls are in? Well I tried "MovieClip" and stage.myClip.TextField and got loads of errors.
I'm back in the bottom of coding skills, thanks to as3. :(
Do this:
Make a new Flash file, insert a new blank MovieClip, go to the file that contains the scrollbar and select all the layers press Ctrl + Alt+ C (Copy Frames). Now go back to the created file select the only layer and press Ctrl+Alt+V( Paste Frames) and now you have the scrollbar nested in a new MovieClip.
The field and the scrollbar is nested in the third MC from root. How do I target it?
Something like this:
stage.firstMc.secondMc.thirdMc.myTfield.removeEventListener(Event.SCROLL,updateScrollHandler);
1120: Access of undefined property firstMc.
I have frankensteined together a scroll bar with buttons; but I am having trouble with the buttons correlating to the scroll bar's movement.
i.e. the bar sits at the top while the buttons move the text field independently. Any suggestions on how to fix this error?
Very grateful for your tutorial, but unfortunately, I did everything you said... Pasted the code in, removed all the numbers, but had these errors.. (Even double-checked to make sure nothing which shouldn't be removed was removed, tried again etc). Code I used was :
var myMessage:String = "If you like the scroll bar and
want to know how I made it I have posted a detailed tutorial
on my blog: http://biochimistu.blogspot.com.Thanks and
enjoy! If you like the scroll bar and want to know how I made it
I have posted a detailed tutorial on my blog:
http://biochimistu.blogspot.com.Thanks and enjoy!
If you like the scroll bar and want to know how I made it
I have posted a detailed tutorial on my blog:
http://biochimistu.blogspot.com.Thanks and enjoy!
If you like the scroll bar and want to know how I made it
I have posted a detailed tutorial on my blog:
http://biochimistu.blogspot.com.Thanks and enjoy!
If you like the scroll bar and want to know how I made it
I have posted a detailed tutorial on my blog:
http://biochimistu.blogspot.com.Thanks and enjoy!
If you like the scroll bar and want to know how I made it
I have posted a detailed tutorial on my blog:
http://biochimistu.blogspot.com.Thanks and enjoy!
If you like the scroll bar and want to know how I made it
I have posted a detailed tutorial on my blog:
http://biochimistu.blogspot.com.Thanks and enjoy!";
myTfield.text = myMessage;
myTfield.wordWrap = true;
var minScroll:Number = scrollHandler_mc.y;
var maxScroll:Number = minScroll+(track_mc.height -scrollHandler_mc.height);
var scrollInterval:Number = maxScroll - minScroll;
var draging:Boolean = false;
var bounds:Rectangle = new Rectangle(scrollHandler_mc.x,scrollHandler_mc.y,0,track_mc.height-scrollHandler_mc.height);
scrollHandler_mc.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
function beginDrag(event:MouseEvent):void {
scrollHandler_mc.startDrag(false,bounds);
draging = true;
scrollHandler_mc.addEventListener(Event.ENTER_FRAME,checkingProgress);
}
function endDrag(event:MouseEvent):void {
scrollHandler_mc.stopDrag();
draging = false;
}
function checkingProgress(event:Event):void {
var moveDrag:Number = scrollHandler_mc.y - minScroll;
var procentDrag = moveDrag/scrollInterval;
if (draging) {
myTfield.scrollV = procentDrag*myTfield.maxScrollV;
}
}
Errors were :
1095: Syntax error: A string literal must be terminated before the line break.
1083: Syntax error: end of program is unexpected.
--------
Would be lovely to have some help with this...
Cheers.
Lin
**Error** Scene=Scene 1, layer=actions, frame=1:Line 22: The class or interface 'MouseEvent' could not be loaded.
function endDrag(event:MouseEvent):void {
**Error** Scene=Scene 1, layer=actions, frame=1:Line 27: The class or interface 'Event' could not be loaded.
function checkingProgress(event:Event):void {
I'm running Flash 8 so I'm assuming that I don't have Actionscript 3.0. Is there a Flash 8 equivalent?
Yes, you can insert paragraph breaks in the text look at the Format text with CSS in ActionScript 3.0. tutorial to see how to format your text. This script wont work to scroll picture.
I’m glad that you have made those script to work together.
Thanks a lot for this tutorial.
I read here, if you had time you will write the same tutorial (with mouse wheel )but with a Images,etc.. scrolling a MC container with images text, etc...
Did you made it?
Thanks lot very useful for the AS3 beginners
Cris
I have written a tutorial, it’s not using the mouse wheel but it explains how to scroll images.
You can check it here: Scrolling photos loaded dynamically with ActionScript 3.
The first error has to do with the string assigned to the myMessage variable, all the string must be in one line. The second error is a syntax error, so you probable have forgot a {, ; or misspelled something.
Also download the source files and take a look.