Tutorials » Interactivity: Useability: ActionScript for Flash 5 dummies: Scrolling a text box - Page 3
Adding the ActionScript
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:
onClipEvent (load){
daTextBox = "Insert text here";
}
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:
on( press ){
scrolling = "up";
}
on( release, releaseOutside ){
scrolling = 0;
}
Down button:
on( press ){
scrolling = "down";
}
on( release, releaseOutside ){
scrolling = 0;
}
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:
onClipEvent (enterFrame){
if( scrolling == "up"){
daTextBox.scroll--;
}
if( scrolling == "down"){
daTextBox.scroll++;
}
}
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!
| » Level Basic |
|
Added: 2001-06-20 Rating: 8 Votes: 400 |
| » Author |
| No details available. |
| » Download |
| Download the files used in this tutorial. |
| Download (12 kb) |
| » Forums |
| More help? Search our boards for quick answers! |
-
You must have javascript enabled in order to post comments.


Comments
There are no comments yet. Be the first to comment!