Freelancer behind 5 1/2 math and physics enthusiast Patrick has a knack for making seemingly simple things overly complicated. Perfect for a tutorial writer. So, you're a designer, and you have very little background in programming. You want to add interactivity to your Flash movies. You've heard of this thing called « ActionScript » , you thought it could do the job. You've checked out the Flash 5 manual, you tried to follow the examples, you tried again, then tried some more, then after a few days, you decided to give up before smashing your keyboard into your brand new 19 inch monitor. Well, this series of tutorials is intended for you.
Using a few classic examples of interactivity, I am going to show how to use ActionScript in a practical manner. I'll try to be as clear and concise as possible. Hopefully, by the end of the series, you'll be able to add interactivity to your Flash movies, with a minimal amount of collateral damage to your surrounding hardware or small pets.
If there's a question that gets asked the most at the Flash 5 board here at Flashkit, then it's probably « How do I scroll a text box » (although « Why can't this stupid piece of **** work? » is not far behind).
Scrolling a text box is not as easy as it seems. It involves quite a bit of ActionScript, which is a bit *intimidating* to the beginner. Well, fear not, using my technique, by the end of the day, you'll be able to scroll a text box just like this:
Okay, first of all you'll need to make two buttons: up and down. They can be of any shape you want. You may want to give the buttons a big hit area so that the user can be slightly off the button and still be able to scroll.
Next you'll need to make a textbox. Just click on the text tool and let your inspiration carry you. You can let the text overflow, we'll fix this later.
Now select the text and go into the « Text Options » panel. Set the options as in this screenshot:

If you can't seem to find the panel, it's in Window > Panels > Text options. Here's what each of the options do:
There is something peculiar in the way Flash handles text boxes and scrolling: you can't scroll a text box unless you fill its contents using ActionScript. In order to do this, you can load external text files into Flash, but that's much beyond the scope of this tutorial. Right now, we're just going to place the contents of the text box directly in Actionscript (a technique called «hard-coding» ).
There's an easy way to transfer the text ( or rather, the underlying HTML ) from the text box to ActionScript. When you test your movie (Ctrl+enter), you can see a list of the variables in the movie by going to the Debug menu and choosing « List Variables ». You should see something similar to this:

As you can see, the HTML content of the text box is accessible (it's on the second-to-last line, in this case). Select all of the HTML, copy it, and then paste it into Notepad or a similar program. Save this for later use.
Once this is done, you can go back to your Flash movie, and safely delete the contents of the text box. You should reduce its height as well, otherwise there won't be a need for scrolling.
Finally, place your text box and your two buttons on the main timeline, select everything, and press F8 to convert to a movie clip. Name it «containerMC» . Our setup is now done.
The first thing you'll want to do is load the text into the text box using ActionScript. Select containerMC and go to the « Object Actions » panel. You can make this panel appear by pressing Ctrl+Alt+A, or by pressing the arrow icon at the bottom right of the Flash window.
Now click on the arrow at the top right of the « Object Actions » panel, and select Expert Mode from the drop-down menu. This will allow you to edit the code directly, instead of having to « fill in the blanks ». Copy and paste this script inside of the window:
[as]onClipEvent (load){
daTextBox = "Insert text here";
}
[/as]
You'll need to replace « Insert text here » with your own text ( which you pasted into Notepad, remember?) .
Note the use of onClipEvent(load) here. Everything that is placed inside a load clip event will be executed only once, when the clip is loaded.
Now that this is done, we have to figure out a way to keep track of whether we should scroll the text up or down, or do nothing at all. So we'll add these actions to the buttons:
Up button:
[as]on( press ){
scrolling = "up";
}
on( release, releaseOutside ){
scrolling = 0;
}
[/as]
Down button:
[as]on( press ){
scrolling = "down";
}
on( release, releaseOutside ){
scrolling = 0;
}
[/as]
So what's happening here is pretty simple: when one of the buttons is pressed, the variable «scrolling» is set to "up" or "down", depending on the button. When the button is released, the variable is set to 0, which is the programmatic equivalent of "no".
Now that we know where we should scroll, it's time to actually do the scrolling. Dynamic text boxes have two properties: scroll and maxscroll. Each of the text lines is numbered, starting with 1. The scroll property is the number of the first line that is shown; you can change its value to get a scrolling effect. Maxscroll is the maximum value of scroll, that is, the total number of lines minus the number of lines shown at once; this one can't be set, just read.
Now, we want the scrolling to occur at regular intervals, so we'll have to execute a few lines of code repeatedly, with a certain amount of time in between every execution. To do this, we'll use the *very* useful onClipEvent( enterFrame ) action. Everything that is inside an enterFrame clip event is executed every single frame.
Knowing all of this, we can add the following script inside of containerMC's actions, below the « load the text » code:
[as]onClipEvent (enterFrame){
if( scrolling == "up"){
daTextBox.scroll--;
}
if( scrolling == "down"){
daTextBox.scroll++;
}
}
[/as]
Simple, isn't it? If you're not familiar with the use of ++ and --, here's a brief explanation. ++ adds 1 to the value of the variable or property it affects; this is known as incrementing the value. --, on the other hand, subtracts 1 from the value of a variable; this is called decrementing a value.
If you test your movie now, you should see the scroller work!
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:
[as]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++;
}
[/as]
For the up button:
[as]on( press ){
scrolling = "up";
frameCounter = speedFactor;
}
on( release, releaseOutside ){
scrolling = 0;
}
[/as]
For the down button:
[as]on( press ){
scrolling = "down";
frameCounter = speedFactor;
}
on( release, releaseOutside ){
scrolling = 0;
}
[/as]
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 [email:patrickmineault@sympatico.ca].
Be sure to be there for the next tutorial. We'll check out how to make a hierarchical menu.